Ejemplo n.º 1
0
        /// <summary>
        /// Call an API using POST method
        /// </summary>
        /// <param name="apiMethodEndpoint">API end point like: /api/users/get_all</param>
        /// <param name="param">key value pair of the body post params</param>
        /// <returns></returns>
        public string Post(string apiMethodEndpoint, Dictionary <string, string> param)
        {
            WebClient client      = new WebClient();
            OAuthBase oauth       = new OAuthBase();
            string    requestBody = "";

            if (param != null && param.Count > 0)
            {
                foreach (KeyValuePair <string, string> keyValuePair in param)
                {
                    requestBody += Uri.EscapeDataString(keyValuePair.Key) + "=" + Uri.EscapeDataString(keyValuePair.Value) + "&";
                }
                requestBody  = requestBody.Trim('&');
                requestBody += Environment.NewLine;
            }
            Uri    uri             = new Uri(BaseUrl + apiMethodEndpoint);
            string bodyHash        = oauth.ComputeHash(HashAlgorithm.Create("SHA1"), requestBody);
            string bodyHashEscaped = Uri.EscapeDataString(bodyHash);
            string timestamp       = oauth.GenerateTimeStamp();
            string nounce          = oauth.GenerateNonce();

            string normalizedUrl;
            string normalizedParams;

            //create signature
            string signature           = oauth.GenerateSignature(uri, Key, Secret, AccessToken.Token, AccessToken.TokenSecret, "POST", timestamp, nounce, OAuthBase.SignatureTypes.HMACSHA1, bodyHashEscaped, null, out normalizedUrl, out normalizedParams);
            string authorizationParams = string.Format(
                "OAuth oauth_body_hash=\"{5}\", oauth_consumer_key=\"{0}\", oauth_nonce=\"{4}\", oauth_signature=\"{2}\",  oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"{3}\", oauth_token=\"{1}\", oauth_version=\"1.0\"",
                Uri.EscapeDataString(Key),
                Uri.EscapeDataString(AccessToken.Token),
                Uri.EscapeDataString(signature),
                Uri.EscapeDataString(timestamp),
                Uri.EscapeDataString(nounce),
                Uri.EscapeDataString(bodyHash)
                );

            client.Headers.Add("Content-Type", "x-www-formurlencoded");
            client.Headers.Add("Authorization", authorizationParams);
            try
            {
                var result = client.UploadString(uri, requestBody);
                return(result);
            }
            catch (WebException ex)
            {
                ProcessException(ex);
                throw; //todo add exception
            }
        }
Ejemplo n.º 2
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
            }
        }