Google Contacts API - After getting the access tok

2020-06-23 06:04发布

问题:

I managed to get the access token for google's contacts API but when i try to make a call to retrieve the logged in user's profile, i get a 401 unauthorized error...

I did some research and followed the steps mentioned in "various" google documentations ( like this one and this one and many others) but with no use...

So far i think i'm signing the request wrong. Here's what i'm doing after i get the access token.

string outUrl,querystring;
string sig = oAuth.GenerateSignature(new Uri("https://www.google.com/m8/feeds/contacts/default/full"), Server.UrlEncode(oAuth.ConsumerKey), oAuth.ConsumerSecret, oAuth.Token, null, "GET", timeStamp, nonce, out outUrl, out querystring);
string reqURL = "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + oAuth.Token + "&oauth_signature_method=HMAC-SHA1&oauth_signature=" + Server.UrlEncode(sig) + "&oauth_consumer_key=" + oAuth.ConsumerKey + "&oauth_timestamp=" + timeStamp + "&oauth_nonce=" + nonce + "&oauth_version=1.0";
response = oAuth.WebRequest(oAuthGoogle.Method.GET, reqURL, String.Empty);

The 401 error appears when send the request using oAuth.WebRequest() (the last line of the code above)

I just need to get rid of the 401 error...I'm using ASP.NET/C#. Any help would be appreciated. Thank you...

回答1:

Your code example defines reqURL which is not used and uses url which is not defined.

You would normally provide OAuth request parameters with the authorization header rather than the querystring.

http://oauth.net/core/1.0/#auth_header_authorization

I would imagine signing the request and setting the Authorization this is something that's being handled inside your OAuth object.

To clarify

I have used a method like this to sign http requests in my OAuth 1.0a implementation:

    /// <summary>
    /// Gets the authorization header.
    /// </summary>
    /// <param name="method">The method.</param>
    /// <param name="url">The URL of the request.</param>
    /// <param name="parameters">The parameters.</param>
    /// <returns>Authorization header</returns>
    public string GetAuthorizationHeader(string method, Uri url, NameValueCollection parameters)
    {
        parameters.Set("oauth_consumer_key", this.ConsumerKey);
        parameters.Set("oauth_nonce", this.GetNonce());
        parameters.Set("oauth_timestamp", this.GetTimeStamp());
        parameters.Set("oauth_version", "1.0");
        parameters.Set("oauth_signature_method", "HMAC-SHA1");

        string signString = this.GetSignString(method, url, parameters);
        string signature = this.GetSignature(signString, this.ConsumerSecret, this.tokenSecret);

        parameters.Set("oauth_signature", signature);

        StringBuilder authorizationHeader = new StringBuilder();
        foreach (string paramKey in parameters.AllKeys)
        {
            if (authorizationHeader.Length > 0)
            {
                authorizationHeader.Append(", ");
            }
            else
            {
                authorizationHeader.Append("OAuth ");
            }

            authorizationHeader.AppendFormat("{0}=\"{1}\"", paramKey, OAuthHelper.UrlEncode(parameters[paramKey]));
        }

        return authorizationHeader.ToString();
    }

Which I use like this

    public void SignHttpWebRequest(string token, string tokenSecret, ref HttpWebRequest request)
    {
        NameValueCollection parameters = new NameValueCollection();
        this.tokenSecret = tokenSecret;
        parameters.Set("oauth_token", token);
        request.Headers.Add("Authorization", this.GetAuthorizationHeader(request, parameters));
    }