0

I have to Authorize a request using OAuth1.0. In the response header it requires access token,OAuth Nonce,Timestamp and OAuthSignature.I wrote methods for creating Timestamp and OAuthNonce How the OAuthsignature is generated using these parameters?It uses HMAC-SHA1 method for Hashing the signature. How can i create a method for generating an OAuth signture key. Can anyone suggest a method foe creating the signature using these parameters? Thanks in Advance.

private static string CreateOAuthTimestamp()
        {
            var nowUtc = DateTime.UtcNow;
            var timeSpan = nowUtc - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
            return timestamp;
        }

 private string CreateOauthNonce()
        {
            return Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        }

1 Answers1

0

I found a metod for creating an Oauth 1.0 signture.It requires all parameters of the api request to be hashed in SHA1 algorithm.

private string CreateOauthSignature
       (string resourceUrl, string oauthNonce, string oauthTimestamp , 
SortedDictionary<string, string> requestParameters)
    {
        //Add the standard oauth parameters to the sorted list        
        requestParameters.Add("oauth_consumer_key", consumerKey);
        requestParameters.Add("oauth_nonce", oauthNonce);
        requestParameters.Add("oauth_signature_method", OauthSignatureMethod);
        requestParameters.Add("oauth_timestamp", oauthTimestamp);
        requestParameters.Add("oauth_token", accessToken);
        requestParameters.Add("oauth_version", OauthVersion);

        var sigBaseString = requestParameters.ToWebString();

        var signatureBaseString = string.Concat
        ("GET", "&", Uri.EscapeDataString(resourceUrl), "&",
                            Uri.EscapeDataString(sigBaseString.ToString()));

        //Using this base string, encrypt the data using a composite of the 
        //secret keys and the HMAC-SHA1 algorithm.
        var compositeKey = string.Concat(Uri.EscapeDataString(consumerKeySecret), "&",
                                         Uri.EscapeDataString(accessTokenSecret));

        string oauthSignature;
        using (var hasher = new HMACSHA1(Encoding.ASCII.GetBytes(compositeKey)))
        {
            oauthSignature = Convert.ToBase64String(
                hasher.ComputeHash(Encoding.ASCII.GetBytes(signatureBaseString)));
        }
        return oauthSignature;
    }  

The ToWebstring() is an Extension method for converting the Sorted dictionary to a web string and encoding the special characters.After creating the signature this can be included in the authorization header of the Http request along with the other header parameters viz. nonce,timestamp,access token etc.