Exemplo n.º 1
0
        public BaseToken GetAccessToken(BaseToken requestToken, string verifier)
        {
            string    normalizedUrl    = "";
            string    normalizedParams = "";
            OAuthBase oauth            = new OAuthBase();
            string    timestamp        = oauth.GenerateTimeStamp();
            string    nounce           = oauth.GenerateNonce();

            //create uri
            string requestBody     = "";
            string bodyHash        = oauth.ComputeHash(HashAlgorithm.Create("SHA1"), requestBody);
            string bodyHashEscaped = Uri.EscapeDataString(bodyHash);
            //create signature
            string signature = oauth.GenerateSignature(GetAccessTokenUri(), Key, Secret, requestToken.Token, requestToken.TokenSecret, "POST", timestamp, nounce, OAuthBase.SignatureTypes.HMACSHA1, bodyHashEscaped, verifier, out normalizedUrl, out normalizedParams);

            try
            {
                //call remote server to get token
                WebClient client = new WebClient();
                string    authorizationParams =
                    string.Format(
                        "OAuth oauth_body_hash=\"{6}\", oauth_consumer_key=\"{0}\", oauth_nonce=\"{4}\", oauth_signature=\"{2}\",  oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"{3}\", oauth_token=\"{1}\", oauth_verifier=\"{5}\", oauth_version=\"1.0\"",
                        Uri.EscapeDataString(Key),
                        Uri.EscapeDataString(requestToken.Token),
                        Uri.EscapeDataString(signature),
                        Uri.EscapeDataString(timestamp),
                        Uri.EscapeDataString(nounce),
                        Uri.EscapeDataString(verifier),
                        Uri.EscapeDataString(bodyHash)
                        );
                client.Headers.Add("Authorization", authorizationParams);
                var result = client.UploadString(GetAccessTokenUri(), requestBody);
                AccessToken = ParseAuthTokenResponse(result);
                return(AccessToken);
            } catch (WebException ex) {
                ProcessException(ex);
                throw; //todo add exception
            }
        }
Exemplo n.º 2
0
        public BaseToken GetRequestToken()
        {
            try
            {
                string    normalizedUrl;
                string    normalizedParams;
                OAuthBase oauth = new OAuthBase();

                string timestamp = oauth.GenerateTimeStamp();
                string nounce    = oauth.GenerateNonce();
                string signature = oauth.GenerateSignature(GetRequestTokenUri(), Key, Secret, null, null, "GET", timestamp, nounce, OAuthBase.SignatureTypes.HMACSHA1, null, null, out normalizedUrl, out normalizedParams);

                //call remote server to get token
                WebClient client = new WebClient();
                var       result = client.DownloadString(normalizedUrl + "?" + normalizedParams + "&oauth_signature=" + signature);
                return(ParseAuthTokenResponse(result));
            }
            catch (WebException ex)
            {
                ProcessException(ex);
                throw; //todo add exception
            }
        }