public ITwitterCredentials GenerateToken(IConsumerCredentials appCredentials) { var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", appCredentials.VerifierCode, true, true, false); try { var authHandler = new AuthHttpHandler(callbackParameter); var response = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler, new TwitterCredentials(appCredentials)); if (response == null) { return(null); } Match responseInformation = Regex.Match(response, "oauth_token=(?<oauth_token>(?:\\w|\\-)*)&oauth_token_secret=(?<oauth_token_secret>(?:\\w)*)&user_id=(?<user_id>(?:\\d)*)&screen_name=(?<screen_name>(?:\\w)*)"); var credentials = new TwitterCredentials(); credentials.AccessToken = responseInformation.Groups["oauth_token"].Value; credentials.AccessTokenSecret = responseInformation.Groups["oauth_token_secret"].Value; credentials.ConsumerKey = appCredentials.ConsumerKey; credentials.ConsumerSecret = appCredentials.ConsumerSecret; return(credentials); } catch (TwitterException ex) { LogExceptionOrThrow(ex); } return(null); }
// Step 2 - Generate User Credentials public ITwitterCredentials GetCredentialsFromVerifierCode(string verifierCode, IAuthenticationToken authToken) { try { if (authToken == null) { throw new ArgumentNullException("Authentication Token cannot be null."); } if (verifierCode == null) { throw new ArgumentNullException("VerifierCode", "If you've received a verifier code that is null, " + "it means that authentication has failed!"); } var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true, true, false); var authHandler = new AuthHttpHandler(callbackParameter, authToken); var response = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler, new TwitterCredentials(authToken.ConsumerCredentials)); if (response == null) { return(null); } var responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex); if (responseInformation.Groups["oauth_token"] == null || responseInformation.Groups["oauth_token_secret"] == null) { return(null); } var credentials = new TwitterCredentials( authToken.ConsumerKey, authToken.ConsumerSecret, responseInformation.Groups["oauth_token"].Value, responseInformation.Groups["oauth_token_secret"].Value); return(credentials); } catch (TwitterException ex) { if (_exceptionHandler.LogExceptions) { _exceptionHandler.AddTwitterException(ex); } if (!_exceptionHandler.SwallowWebExceptions) { throw; } } return(null); }
// Step 1 - Generate Authorization URL public IAuthenticationContext InitAuthenticationProcess(IConsumerCredentials appCredentials, string callbackURL, string credsIdentifier) { try { var authContext = new AuthenticationContext(appCredentials); var token = authContext.Token; if (string.IsNullOrEmpty(callbackURL)) { callbackURL = Resources.OAuth_PINCode_CallbackURL; } else if (credsIdentifier != null) { _credentialsStore.CallbackAuthenticationContextStore.Add(credsIdentifier, authContext); callbackURL = callbackURL.AddParameterToQuery(Resources.RedirectRequest_CredsParamId, credsIdentifier); } var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_callback", callbackURL, true, true, false); var authHandler = new AuthHttpHandler(callbackParameter, authContext.Token); var requestTokenResponse = _twitterRequestHandler.ExecuteQuery( Resources.OAuthRequestToken, HttpMethod.POST, authHandler, new TwitterCredentials(appCredentials)); if (!string.IsNullOrEmpty(requestTokenResponse) && requestTokenResponse != Resources.OAuthRequestToken) { Match tokenInformation = Regex.Match(requestTokenResponse, Resources.OAuthTokenRequestRegex); bool callbackConfirmed; if (!bool.TryParse(tokenInformation.Groups["oauth_callback_confirmed"].Value, out callbackConfirmed) || !callbackConfirmed) { return(null); } token.AuthorizationKey = tokenInformation.Groups["oauth_token"].Value; token.AuthorizationSecret = tokenInformation.Groups["oauth_token_secret"].Value; authContext.AuthorizationURL = string.Format("{0}?oauth_token={1}", Resources.OAuthRequestAuthorize, token.AuthorizationKey); authContext.Token.AuthorizationUniqueIdentifier = credsIdentifier; return(authContext); } } catch (TwitterException ex) { LogExceptionOrThrow(ex); } return(null); }
// Step 1 - Generate Authorization URL public string GetAuthorizationURL(IConsumerCredentials appCredentials, string callbackURL, bool updateQueryIsAuthorized) { try { if (string.IsNullOrEmpty(callbackURL)) { callbackURL = Resources.OAuth_PINCode_CallbackURL; } else if (updateQueryIsAuthorized) { var credsIdentifier = Guid.NewGuid(); _credentialsStore.CallbackCredentialsStore.Add(credsIdentifier, appCredentials); callbackURL = callbackURL.AddParameterToQuery(Resources.RedirectRequest_CredsParamId, credsIdentifier.ToString()); } var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_callback", callbackURL, true, true, false); var authHandler = new AuthHttpHandler(callbackParameter); var requestTokenResponse = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestToken, HttpMethod.POST, authHandler, new TwitterCredentials(appCredentials)); if (!string.IsNullOrEmpty(requestTokenResponse) && requestTokenResponse != Resources.OAuthRequestToken) { Match tokenInformation = Regex.Match(requestTokenResponse, Resources.OAuthTokenRequestRegex); bool callbackConfirmed = Boolean.Parse(tokenInformation.Groups["oauth_callback_confirmed"].Value); if (!callbackConfirmed) { return(null); } appCredentials.AuthorizationKey = tokenInformation.Groups["oauth_token"].Value; appCredentials.AuthorizationSecret = tokenInformation.Groups["oauth_token_secret"].Value; return(String.Format("{0}?oauth_token={1}", Resources.OAuthRequestAuthorize, appCredentials.AuthorizationKey)); } } catch (TwitterException ex) { LogExceptionOrThrow(ex); } return(null); }
// Step 2 - Generate User Credentials public ITwitterCredentials GetCredentialsFromVerifierCode(string verifierCode, IConsumerCredentials appCredentials) { try { var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true, true, false); var authHandler = new AuthHttpHandler(callbackParameter); var response = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler, new TwitterCredentials(appCredentials)); if (response == null) { return(null); } Match responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex); if (responseInformation.Groups["oauth_token"] == null || responseInformation.Groups["oauth_token_secret"] == null) { return(null); } var credentials = new TwitterCredentials( appCredentials.ConsumerKey, appCredentials.ConsumerSecret, responseInformation.Groups["oauth_token"].Value, responseInformation.Groups["oauth_token_secret"].Value); return(credentials); } catch (TwitterException ex) { if (_exceptionHandler.LogExceptions) { _exceptionHandler.AddTwitterException(ex); } if (!_exceptionHandler.SwallowWebExceptions) { throw; } } return(null); }