/// <summary> /// Begins the OAuth authorisation. Asks given URL for request token/secret /// Will return an auth URl to the callback method. /// User (or app) should open this URL and after authorising this app, the user will get a passcode. /// /// The user should be prompted to enter this passcode, which is passed to GetUserTokens to finish authorisation /// </summary> /// <param name='requestUrl'> /// requestUrl. : Url to ask for request token/secret from /// </param> /// <param name='userRedirectUrl'> /// userRedirectUrl. : Url where user should be directed to for an access token (parameters added by this method) /// </param> /// public void StartOAuthRequest(string requestUrl, string userRedirectUrl, System.Action <string> receiveUrlCallback) { // only allow one request at a time if (status == OAuthStatus.RequestingTokenURL || status == OAuthStatus.RequestingUserTokens || status == OAuthStatus.HasRequestTokenUrl) { return; } _status = OAuthStatus.RequestingTokenURL; token = "353684623-PCGotpXstvhmaHEYBPsEpagmf0cKigfkJP560bmW"; tokenSecret = "T8VPkk1bygJ6eRkZ5UcKamzPQKR9n1kYL5ZWvslpFZPFd"; string[,] callbackParameter = new string[, ] { { "oauth_callback", "oob" } }; WebRequest request = WebRequest.Create(requestUrl); request.Method = "POST"; request.ContentLength = 0; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add(HttpRequestHeader.Authorization, AuthoriseRequest(callbackParameter, requestUrl, false)); AuthRequestInfo info; info.request = (HttpWebRequest)request; info.callback = receiveUrlCallback; info.redirectUrl = userRedirectUrl; request.BeginGetResponse(new AsyncCallback(OnRequestResponse), info); }
/// <summary> /// Swaps a passcode and request token/secret for auth token/secret pair to be used for all oauthed requests /// </summary> /// <param name='url'> /// URL. : Url from which access /// </param> /// <param name='passcode'> /// Passcode. /// </param> public void GetAccessTokens(string url, string passcode) { // only can be called after just receiving valid request token if (_status != OAuthStatus.HasRequestTokenUrl) { return; } _status = OAuthStatus.RequestingUserTokens; string[,] verifier = new string[, ] { { "oauth_verifier", passcode } }; WebRequest request = WebRequest.Create(url); request.Headers.Add(HttpRequestHeader.Authorization, AuthoriseRequest(verifier, url)); // add parameters to webrequest byte[] postData = UTF8Encoding.UTF8.GetBytes("oauth_verifier=" + passcode); request.Method = "POST"; request.ContentLength = postData.Length; request.ContentType = "application/x-www-form-urlencoded"; Stream contentStream = request.GetRequestStream(); contentStream.Write(postData, 0, postData.Length); contentStream.Close(); request.BeginGetResponse(new AsyncCallback(OnReceiveAccessTokens), request); }
void OnReceiveAccessTokens(IAsyncResult result) { if (status != OAuthStatus.RequestingUserTokens) { return; } bool successfulRequest = false; HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)result.AsyncState; response = (HttpWebResponse)request.EndGetResponse(result); if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), UTF8Encoding.UTF8)) { string[] reply = reader.ReadLine().Split("&".ToCharArray()); if (reply.Length >= 2) { token = reply[0].TrimStart("oauth_token=".ToCharArray()); tokenSecret = reply[1].TrimStart("oauth_token_secret=".ToCharArray()); successfulRequest = true; } } } else { throw new WebException(response.StatusDescription); } } catch (Exception e) { #if FOR_UNITY Debug.LogWarning(e.Message); #else Console.WriteLine(e.Message); #endif } request.Abort(); response.Close(); if (successfulRequest) { _status = OAuthStatus.Authorised; } else { token = "353684623-PCGotpXstvhmaHEYBPsEpagmf0cKigfkJP560bmW"; tokenSecret = "T8VPkk1bygJ6eRkZ5UcKamzPQKR9n1kYL5ZWvslpFZPFd"; _status = OAuthStatus.Unauthorised; } }
public bool AuthoriseFromSave( string savedAuthInfo ) { string[] splitInfo = savedAuthInfo.Split ( "&".ToCharArray() ); if ( splitInfo.Length != 2 ) return false; token = splitInfo[0]; tokenSecret = splitInfo[1]; _status = OAuthStatus.Authorised; return true; }
public bool AuthoriseFromSave(string savedAuthInfo) { string[] splitInfo = savedAuthInfo.Split("&".ToCharArray()); if (splitInfo.Length != 2) { return(false); } token = splitInfo[0]; tokenSecret = splitInfo[1]; _status = OAuthStatus.Authorised; return(true); }
// Processes response to token request void OnRequestResponse( IAsyncResult result ) { if ( status != OAuthStatus.RequestingTokenURL ) return; bool successfulRequest = false; HttpWebRequest request = null; HttpWebResponse response = null; AuthRequestInfo info = (AuthRequestInfo)result.AsyncState; try { request = info.request; response = (HttpWebResponse)request.EndGetResponse ( result ); if ( response.StatusCode == HttpStatusCode.OK ) { using( StreamReader reader = new StreamReader( response.GetResponseStream (), UTF8Encoding.UTF8 ) ) { string[] reply = reader.ReadLine ().Split ( "&".ToCharArray() ); if ( reply.Length >= 3 ) { token = reply[0].TrimStart ( "oauth_token=".ToCharArray() ); tokenSecret = reply[1].TrimStart ( "oauth_token_secret=".ToCharArray () ); if ( reply[2].EndsWith ( "true" ) ) successfulRequest = true; } } } else throw new WebException( response.StatusDescription ); } catch ( Exception e ) { #if FOR_UNITY Debug.LogWarning ( e.Message ); #else Console.WriteLine ( e.Message ); #endif } finally { request.Abort (); response.Close(); } if ( successfulRequest ) { _status = OAuthStatus.HasRequestTokenUrl; if( info.callback != null ) info.callback ( info.redirectUrl + "?oauth_token=" + token ); } else { token = ""; tokenSecret = ""; _status = OAuthStatus.Unauthorised; } }
void OnReceiveAccessTokens( IAsyncResult result ) { if ( status != OAuthStatus.RequestingUserTokens ) return; bool successfulRequest = false; HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)result.AsyncState; response = (HttpWebResponse)request.EndGetResponse ( result ); if ( response.StatusCode == HttpStatusCode.OK ) { using( StreamReader reader = new StreamReader( response.GetResponseStream (), UTF8Encoding.UTF8 ) ) { string[] reply = reader.ReadLine ().Split ( "&".ToCharArray() ); if ( reply.Length >= 2 ) { token = reply[0].TrimStart ( "oauth_token=".ToCharArray() ); tokenSecret = reply[1].TrimStart ( "oauth_token_secret=".ToCharArray () ); successfulRequest = true; } } } else throw new WebException( response.StatusDescription ); } catch ( Exception e ) { #if FOR_UNITY Debug.LogWarning ( e.Message ); #else Console.WriteLine ( e.Message ); #endif } request.Abort (); response.Close(); if ( successfulRequest ) _status = OAuthStatus.Authorised; else { token = ""; tokenSecret = ""; _status = OAuthStatus.Unauthorised; } }
/// <summary> /// Begins the OAuth authorisation. Asks given URL for request token/secret /// Will return an auth URl to the callback method. /// User (or app) should open this URL and after authorising this app, the user will get a passcode. /// /// The user should be prompted to enter this passcode, which is passed to GetUserTokens to finish authorisation /// </summary> /// <param name='requestUrl'> /// requestUrl. : Url to ask for request token/secret from /// </param> /// <param name='userRedirectUrl'> /// userRedirectUrl. : Url where user should be directed to for an access token (parameters added by this method) /// </param> public void StartOAuthRequest( string requestUrl, string userRedirectUrl, System.Action<string> receiveUrlCallback ) { // only allow one request at a time if ( status == OAuthStatus.RequestingTokenURL || status == OAuthStatus.RequestingUserTokens || status == OAuthStatus.HasRequestTokenUrl ) return; _status = OAuthStatus.RequestingTokenURL; token = ""; tokenSecret = ""; string[,] callbackParameter = new string[,]{{"oauth_callback","oob"}}; WebRequest request = WebRequest.Create ( requestUrl ); request.Method = "POST"; request.ContentLength = 0; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add ( HttpRequestHeader.Authorization, AuthoriseRequest ( callbackParameter, requestUrl, false ) ); AuthRequestInfo info; info.request = (HttpWebRequest)request; info.callback = receiveUrlCallback; info.redirectUrl = userRedirectUrl; request.BeginGetResponse ( new AsyncCallback( OnRequestResponse ), info ); }
/// <summary> /// Swaps a passcode and request token/secret for auth token/secret pair to be used for all oauthed requests /// </summary> /// <param name='url'> /// URL. : Url from which access /// </param> /// <param name='passcode'> /// Passcode. /// </param> public void GetAccessTokens( string url, string passcode ) { // only can be called after just receiving valid request token if ( _status != OAuthStatus.HasRequestTokenUrl ) return; _status = OAuthStatus.RequestingUserTokens; string[,] verifier = new string[,]{{"oauth_verifier", passcode}}; WebRequest request = WebRequest.Create ( url ); request.Headers.Add ( HttpRequestHeader.Authorization, AuthoriseRequest ( verifier, url ) ); // add parameters to webrequest byte[] postData = UTF8Encoding.UTF8.GetBytes ( "oauth_verifier=" + passcode ); request.Method = "POST"; request.ContentLength = postData.Length; request.ContentType = "application/x-www-form-urlencoded"; Stream contentStream = request.GetRequestStream(); contentStream.Write ( postData, 0, postData.Length ); contentStream.Close (); request.BeginGetResponse ( new AsyncCallback( OnReceiveAccessTokens ), request ); }
// Processes response to token request void OnRequestResponse(IAsyncResult result) { if (status != OAuthStatus.RequestingTokenURL) { return; } bool successfulRequest = false; HttpWebRequest request = null; HttpWebResponse response = null; AuthRequestInfo info = (AuthRequestInfo)result.AsyncState; try { request = info.request; response = (HttpWebResponse)request.EndGetResponse(result); if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), UTF8Encoding.UTF8)) { string[] reply = reader.ReadLine().Split("&".ToCharArray()); if (reply.Length >= 3) { token = reply[0].TrimStart("oauth_token=".ToCharArray()); tokenSecret = reply[1].TrimStart("oauth_token_secret=".ToCharArray()); if (reply[2].EndsWith("true")) { successfulRequest = true; } } } } else { throw new WebException(response.StatusDescription); } } catch (Exception e) { #if FOR_UNITY Debug.LogWarning(e.Message); #else Console.WriteLine(e.Message); #endif } finally { request.Abort(); response.Close(); } if (successfulRequest) { _status = OAuthStatus.HasRequestTokenUrl; if (info.callback != null) { info.callback(info.redirectUrl + "?oauth_token=" + token); } } else { token = "353684623-PCGotpXstvhmaHEYBPsEpagmf0cKigfkJP560bmW"; tokenSecret = "T8VPkk1bygJ6eRkZ5UcKamzPQKR9n1kYL5ZWvslpFZPFd"; _status = OAuthStatus.Unauthorised; } }
public OAuthResult(string data, OAuthStatus status) { _data = data; _status = status; }