Skip to content

Instantly share code, notes, and snippets.

@Kogarasi
Created January 4, 2013 10:37
Show Gist options
  • Save Kogarasi/4451571 to your computer and use it in GitHub Desktop.
Save Kogarasi/4451571 to your computer and use it in GitHub Desktop.
//================================================================================
//! @file OAuth_v1.cs
//! @brief OAuth認証
//! @date 07/25/2012
//! @author kogarasi
//================================================================================
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
//==================================================
//! @brief OAuth認証クラス
//! @note ...
//==================================================
public class OAuth_v1 : MonoBehaviour
{
/*! OAuth認証で使用するコンシューマキー */
[SerializeField]
public string ConsumerToken = null; //!< "Consumer key"
[SerializeField]
public string ConsumerSecret = null; //!< "Consumer secret"
/*! OAuth認証で使用するアクセストークン */
private string AccessToken = null;
private string AccessSecret = null;
//--------------------------------------------------
//! @brief アクセストークンの設定
//! @param token AccessToken
//! @param secret AccessSecret
//! @note ...
//--------------------------------------------------
protected void SetAccessKeys( string token, string secret )
{
AccessToken = token;
AccessSecret = secret;
}
//--------------------------------------------------
//! @brief コンシューマキーが設定されているか
//! @return 設定されていればTRUE
//! @note ...
//--------------------------------------------------
private bool isSettedConsumer()
{
return ( ConsumerToken != null && ConsumerSecret != null );
}
//--------------------------------------------------
//! @brief 認証用のパラメータを生成
//! @return 認証用のパラメータ
//! @note ...
//--------------------------------------------------
protected SortedDictionary<string,string> CreateOAuthParameters()
{
SortedDictionary<string,string> sortDictionary = new SortedDictionary<string,string>();
if( ! isSettedConsumer() )
{
Debug.Log( "Please set Consumer key or Consumer secret" );
return sortDictionary;
}
sortDictionary.Add( "oauth_consumer_key", ConsumerToken );
sortDictionary.Add( "oauth_token", AccessToken != null ? AccessToken : "" );
sortDictionary.Add( "oauth_nonce", "" + DateTime.Now.Millisecond );
sortDictionary.Add( "oauth_timestamp", "" + ThreeRings.Utility.GetUnixTime( DateTime.Now ) );
sortDictionary.Add( "oauth_version", "1.0" );
sortDictionary.Add( "oauth_signature_method", "HMAC-SHA1" );
return sortDictionary;
}
//--------------------------------------------------
//! @brief 認証に使うシグネチャを生成
//! @param Method リクエストの際のメソッドタイプを指定
//! @param URL リクエストを行うURL
//! @param Parameters リクエストの際のパラメータ
//! @return シグネチャコード
//! @note ...
//--------------------------------------------------
protected string CreateSignature( string Method, string URL, SortedDictionary<string, string > Parameters )
{
if( ! isSettedConsumer() )
{
Debug.Log( "Please set Consumer key or Consumer secret" );
return "";
}
// URLとパラメータはエスケープをする
URL = Uri.EscapeDataString( URL );
string ParamString = Uri.EscapeDataString( ThreeRings.Utility.CreateQueryParameter( Parameters ) );
Debug.Log( "PARAM:" + ParamString );
string BaseString = Method + "&" + URL + "&" + ParamString;
Debug.Log( "BASE_STRING:" + BaseString );
// ハッシュ生成用
byte[] data = System.Text.Encoding.UTF8.GetBytes( BaseString );
byte[] key = System.Text.Encoding.ASCII.GetBytes( ConsumerSecret + "&" );
// HMAC-SHA1ハッシュを生成
System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1( key );
byte[] hash = hmac.ComputeHash( data );
// 最後にBASE64エンコードをして終わり
return Uri.EscapeDataString( Convert.ToBase64String( hash ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment