2011-09-23 40 views
1

struggling to authenticate在Google+ API後,我終於成功找回我的信息與https://www.googleapis.com/plus/v1/people/ {MyUserId}?關鍵= {ApiKey}Google+的API讓人們「我」

然而,儘管我沒有獲得與訪問令牌範圍https://www.googleapis.com/auth/plus.me我不能請求「我」:https://www.googleapis.com/plus/v1/people/me?key= {ApiKey}

我最終得到401未經授權的請求......這是遲到了,我沒有得到我想念的東西。

這裏是我的代碼:

 string apiUrl = "https://www.googleapis.com/plus/v1/people/me?key={my api key"; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl); 
     request.Method = HttpMethod.GET.ToString(); 

     try 
     { 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       => 401 error 

謝謝您的幫助!

+0

嘗試排除API密鑰,一旦您獲得了OAuth令牌 - [People:get](http://developers.google.com/+/api/latest/people/get#examples) _:「」如果使用userId值「me」,則此方法需要使用已授予OAuth範圍的令牌進行認證「」._ [OAuth](http://developers.google.com/+/api/oauth ):_「」如果請求需要授權(例如對個人私人數據的請求),則它必須包含OAuth 2.0令牌。它也可能包含API密鑰,但它不一定要。「」_ – Smudge202

+1

您需要像這樣請求:'「https://www.googleapis.com/plus/v1/people/me?key={api key}&access_token = {access token}「' –

回答

0

我已經3年了,但是我在找到一個幾乎完全相同的問題的解決方案時發現了這篇文章。然而,我試圖避免使用Google的API,因此我想我會發布自己的解決方案,以防有人想要在沒有Google API的情況下看到這種方式。我正在使用RestSharp處理HTTP請求,但這不是必需的。

//craft the request 
string requestUrl = "https://www.googleapis.com/plus/v1/people/me?access_token=" + AccessToken; 
RestClient rc = new RestClient(); 
RestRequest request = new RestRequest(requestUrl, Method.GET); 
request.AddHeader("Content-Type", "application/json"); 
request.AddHeader("x-li-format", "json"); 
request.RequestFormat = DataFormat.Json; 

//send the request, and check the response. 
RestResponse restResponse = (RestResponse)rc.Execute(request); 
if (!restResponse.ResponseStatus.Equals(ResponseStatus.Completed)) { return null; } 

與問題中原始實現的主要區別在於我沒有使用RestSharp - 這是不重要的。主要區別在於我在查詢字符串中傳遞了access_token而不是密鑰。

相關問題