public static async Task <string> StartUserAuthentication(this IUserSession session) { if (string.IsNullOrWhiteSpace(session.clientID)) { throw new ArgumentException("ClientID must be specified", session.clientID); } if (string.IsNullOrWhiteSpace(session.clientSecret)) { throw new ArgumentException("ClientSecret must be specified", session.clientSecret); } if (session.PlatformAdaptor == null) { throw new ArgumentException("Need a Platform Adaptor"); } var sinceEpoch = session.GenerateTimestamp(); var nonce = session.GenerateNoonce(); var sigBaseStringParams = string.Format( "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method=HMAC-SHA1&oauth_timestamp={2}&oauth_version=1.0", session.clientID, nonce, sinceEpoch); var sigBaseString = string.Format("POST&{0}&{1}", TwitterApi.RequestTokenUrl().UrlEncode(), sigBaseStringParams.UrlEncode()); var signature = session.GenerateSignature(session.clientSecret, sigBaseString, null); var dataToPost = string.Format( "OAuth realm=\"\", oauth_nonce=\"{0}\", oauth_timestamp=\"{1}\", oauth_consumer_key=\"{2}\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"{3}\"", nonce, sinceEpoch, session.clientID, signature.UrlEncode()); var response = await PostData(TwitterApi.RequestTokenUrl(), dataToPost); if (string.IsNullOrWhiteSpace(response)) { return(null); } var oAuthToken = ""; foreach (var splits in response.Split('&').Select(t => t.Split('='))) { switch (splits[0]) { case "oauth_token": //these tokens are request tokens, first step before getting access tokens oAuthToken = splits[1]; break; case "oauth_token_secret": // not used // var oAuthTokenSecret = splits[1]; break; case "oauth_callback_confirmed": break; } } if (!string.IsNullOrWhiteSpace(oAuthToken)) { session.PlatformAdaptor.DisplayAuthInBrowser(TwitterApi.AuthenticateUrl() + oAuthToken); } return(oAuthToken); }
public static async Task <TwitterCredentials> Authentication(this IUserSession session, string _callbackuri) { if (string.IsNullOrWhiteSpace(session.clientID)) { throw new ArgumentException("ClientID must be specified", session.clientID); } if (string.IsNullOrWhiteSpace(session.clientSecret)) { throw new ArgumentException("ClientSecret must be specified", session.clientSecret); } var sinceEpoch = session.GenerateTimestamp(); var nonce = session.GenerateNoonce(); var sigBaseStringParams = string.Format( "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method=HMAC-SHA1&oauth_timestamp={2}&oauth_version=1.0", session.clientID, nonce, sinceEpoch); var sigBaseString = string.Format("POST&{0}&{1}", TwitterApi.RequestTokenUrl().UrlEncode(), sigBaseStringParams.UrlEncode()); var signature = session.GenerateSignature(session.clientSecret, sigBaseString, null); var dataToPost = string.Format( "OAuth realm=\"\", oauth_nonce=\"{0}\", oauth_timestamp=\"{1}\", oauth_consumer_key=\"{2}\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_signature=\"{3}\"", nonce, sinceEpoch, session.clientID, signature.UrlEncode()); var response = await PostData(TwitterApi.RequestTokenUrl(), dataToPost); if (string.IsNullOrWhiteSpace(response)) { return(TwitterCredentials.Null); } var oauthCallbackConfirmed = false; var oAuthToken = ""; foreach (var splits in response.Split('&').Select(t => t.Split('='))) { switch (splits[0]) { case "oauth_token": //these tokens are request tokens, first step before getting access tokens oAuthToken = splits[1]; break; case "oauth_token_secret": var OAuthTokenSecret = splits[1]; break; case "oauth_callback_confirmed": if (splits[1].ToLower() == "true") { oauthCallbackConfirmed = true; } break; } } if (oauthCallbackConfirmed && !string.IsNullOrWhiteSpace(oAuthToken)) { var authresponse = await session.PlatformAdaptor.AuthWithBroker(TwitterApi.AuthenticateUrl() + oAuthToken, _callbackuri); if (!string.IsNullOrWhiteSpace(authresponse)) { var responseData = authresponse.Substring(authresponse.IndexOf("oauth_token")); string request_token = null; string oauth_verifier = null; String[] keyValPairs = responseData.Split('&'); foreach (var t in keyValPairs) { var splits = t.Split('='); switch (splits[0]) { case "oauth_token": request_token = splits[1]; break; case "oauth_verifier": oauth_verifier = splits[1]; break; } } sinceEpoch = session.GenerateTimestamp(); nonce = session.GenerateNoonce(); sigBaseStringParams = string.Format( "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method=HMAC-SHA1&oauth_timestamp={2}&oauth_token={3}&oauth_version=1.0", session.clientID, nonce, sinceEpoch, request_token); sigBaseString = string.Format("POST&{0}&{1}", TwitterApi.AuthorizeTokenUrl().UrlEncode(), sigBaseStringParams.UrlEncode()); signature = session.GenerateSignature(session.clientSecret, sigBaseString, null); var httpContent = String.Format("oauth_verifier={0}", oauth_verifier); dataToPost = string.Format( "OAuth realm=\"\", oauth_nonce=\"{0}\", oauth_timestamp=\"{1}\", oauth_consumer_key=\"{2}\", oauth_signature_method=\"HMAC-SHA1\", oauth_version=\"1.0\", oauth_token=\"{3}\", oauth_signature=\"{4}\"", nonce, sinceEpoch, session.clientID, request_token, signature.UrlEncode()); response = await PostData(TwitterApi.AuthorizeTokenUrl(), dataToPost, httpContent); if (string.IsNullOrWhiteSpace(response)) { return(TwitterCredentials.Null); //oops something wrong here } var _accessToken = ""; var _accessTokenSecret = ""; var _userId = ""; var _screenName = ""; foreach (var splits in response.Split('&').Select(t => t.Split('='))) { switch (splits[0]) { case "oauth_token": //these tokens are request tokens, first step before getting access tokens _accessToken = splits[1]; break; case "oauth_token_secret": _accessTokenSecret = splits[1]; break; case "user_id": _userId = splits[1]; break; case "screen_name": _screenName = splits[1]; break; } } if (_accessToken != null && _accessTokenSecret != null && _userId != null && _screenName != null) { return(new TwitterCredentials() { ConsumerKey = session.clientID, ConsumerSecret = session.clientSecret, ScreenName = _screenName, Token = _accessToken, TokenSecret = _accessTokenSecret, UserID = Int64.Parse(_userId), Valid = true }); } return(TwitterCredentials.Null); } } return(TwitterCredentials.Null); }