【问题标题】:Google Contacts API - After getting the access token (oauth)Google Contacts API - 获取访问令牌后 (oauth)
【发布时间】:2012-02-12 09:37:55
【问题描述】:

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

我做了一些研究并按照“各种”谷歌文档中提到的步骤(如this onethis one 以及许多其他人)但没有用......

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

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

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

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

【问题讨论】:

    标签: c# asp.net api oauth


    【解决方案1】:

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

    您通常会提供带有授权标头而不是查询字符串的 OAuth 请求参数。

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

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

    澄清

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

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

    我是这样使用的

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

    【讨论】:

    • 很抱歉,连续 9 个小时的工作让我有点头晕。我在 WebRequest 中使用 reqURL。我将编辑我的问题以解决它,感谢您指出它。我将尝试在授权标头中提供参数,看看会发生什么。这让我很困惑,因为我已经做过 twitter、yahoo、LIVE 和许多其他人,我所要做的就是做 http(s)://RequestURL/?access_token=_ACCESS_TOKEN_ 并且它会起作用。我会试试你的建议,谢谢。
    • 谢谢你,为我做了。查看您的代码后,我意识到如何使用授权标头发送请求,而不是使用查询字符串发送请求。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2014-07-15
    • 2023-03-27
    • 2021-01-10
    • 2012-08-03
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多