Google Contacts API - 获取访问令牌后 (oauth) [英] Google Contacts API - After getting the access token (oauth)

查看:27
本文介绍了Google Contacts API - 获取访问令牌后 (oauth)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法获得了 google 联系人 API 的访问令牌,但是当我尝试拨打电话以检索登录用户的个人资料时,我收到了 401 未经授权的错误...

我做了一些研究,并按照各种"谷歌文档中提到的步骤(例如 这个这个以及许多其他) 但没有用...

到目前为止,我认为我签署的请求是错误的.这是我获得访问令牌后正在做的事情.

string outUrl,querystring;字符串 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, outUrl, out querystring);字符串 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);

使用oAuth.WebRequest()发送请求时出现401错误(上面代码的最后一行)

我只需要摆脱 401 错误...我正在使用 ASP.NET/C#.任何帮助,将不胜感激.谢谢...

解决方案

您的代码示例定义了未使用的 reqURL 并使用了未定义的 url.

您通常会使用授权标头而不是查询字符串来提供 OAuth 请求参数.

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

我认为签署请求并设置授权,这是在您的 OAuth 对象中处理的事情.

澄清

我在我的 OAuth 1.0a 实现中使用了这样的方法来签署 http 请求:

///<摘要>///获取授权头.///</总结>///<param name="method">方法.</param>///<param name="url">请求的 URL.</param>///<param name="parameters">参数.</param>///<returns>授权头</returns>公共字符串 GetAuthorizationHeader(字符串方法,Uri url,NameValueCollection 参数){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);字符串签名 = this.GetSignature(signString, this.ConsumerSecret, this.tokenSecret);parameters.Set("oauth_signature", 签名);StringBuilder authorizationHeader = new StringBuilder();foreach (string paramKey in parameters.AllKeys){if (authorizationHeader.Length > 0){authorizationHeader.Append(", ");}别的{authorizationHeader.Append("OAuth ");}authorizationHeader.AppendFormat("{0}=\"{1}\"", paramKey, OAuthHelper.UrlEncode(parameters[paramKey]));}返回authorizationHeader.ToString();}

我是这样用的

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

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...

解决方案

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));
    }

这篇关于Google Contacts API - 获取访问令牌后 (oauth)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆