2012-01-17 149 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