2012-01-17 233 views
4

后,我设法获取谷歌联系API的访问令牌,但当我尝试拨打电话以检索登录用户的配置文件时,我收到401未经授权的错误。 。Google Contacts API - 获取访问令牌(oauth)

我做了一些研究,并遵循“不同”中提到的步骤谷歌单证(如this onethis one等等),但没有用...

到目前为止,我想我请求签名错误。这是我在获取访问令牌后所做的事情。发送使用oAuth.WebRequest()(上述代码的最后一行)

我只需要摆脱401错误的......我的请求时出现

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

的401错误使用ASP.NET/C#。任何帮助,将不胜感激。谢谢...

回答

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

对不起,连续9个小时的工作让我的头脑乱了一下。我在WebRequest中使用reqURL。我将编辑我的问题来解决它,谢谢指出它。我会尝试在授权标题中提供参数并查看会发生什么情况。这让我感到困惑,因为我已经做过twitter,yahoo,LIVE和其他许多其他的事情,我只需要做一个响应就可以做到http(s):// RequestURL /?access_token = _ACCESS_TOKEN_并且它可以工作。我会尝试你的建议,谢谢。 – Reyno 2012-01-17 18:29:58

+0

谢谢,那是为我做的。看完你的代码后,我意识到如何发送带有授权标头的请求,而不是用querystring发送它。非常感谢! – Reyno 2012-01-18 08:20:56