Exemplo n.º 1
0
        public IToken GenerateToken(
            string twitterConfirmationCode,
            string authorizationKey,
            string authorizationSecret,
            string consumerKey,
            string consumerSecret)
        {
            IOAuthCredentials credentials = new OAuthCredentials(authorizationKey,
                                                                 authorizationSecret,
                                                                 consumerKey,
                                                                 consumerSecret);

            IOAuthToken token = new OAuthToken(credentials);

            List <IOAuthQueryParameter> headers = token.GenerateParameters().ToList();

            headers.Add(new OAuthQueryParameter("oauth_verifier", twitterConfirmationCode, true, true, false));

            string response = "";

            WebExceptionHandlingDelegate wex = delegate(WebException exception)
            {
                if (ExceptionUtils.GetWebExceptionStatusNumber(exception) == 401)
                {
                    response = null;
                }
            };

            response = token.ExecuteQueryWithSpecificParameters(Resources.OAuthRequestAccessToken,
                                                                HttpMethod.POST,
                                                                wex,
                                                                headers);

            if (response == null)
            {
                return(null);
            }

            Match responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);

            IToken result = null;

            if (responseInformation.Groups["oauth_token"] != null &&
                responseInformation.Groups["oauth_token_secret"] != null)
            {
                result = new Token(responseInformation.Groups["oauth_token"].Value,
                                   responseInformation.Groups["oauth_token_secret"].Value,
                                   consumerKey,
                                   consumerSecret);
            }

            return(result);
        }