// 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); }
// Concrete Execute public IWebRequestResult ExecuteQuery( string query, HttpMethod method, ITwitterCredentials credentials, HttpContent httpContent = null) { if (query == null) { // When a query is null and has been generated by Tweetinvi it implies that one of the query parameter was invalid throw new ArgumentException("At least one of the arguments provided to the query was invalid."); } return(_twitterRequestHandler.ExecuteQuery(query, method, httpContent: httpContent, credentials: credentials)); }
// Concrete Execute public string ExecuteQuery(string query, HttpMethod method) { if (query == null) { // When a query is null and has been generated by Tweetinvi it implies that one of the query parameter was invalid throw new ArgumentException("At least one of the arguments provided to the query was invalid."); } try { return(_twitterRequestHandler.ExecuteQuery(query, method)); } catch (TwitterException ex) { HandleQueryException(ex); return(null); } }
public async Task <ITwitterResult> ExecuteRequest(ITwitterRequest request) { var response = await _twitterRequestHandler.ExecuteQuery(request).ConfigureAwait(false); return(_twitterResultFactory.Create(request, response)); }