如何調用https://www.googleapis.com/plus/v1/people/me在谷歌?

[英]How to call https://www.googleapis.com/plus/v1/people/me at google


I am developing an Android application and need to get the "me" info from google but I always ends up in either response code 401 or 403. What am I doing wrong? Here is my code:

我正在開發一個Android應用程序,需要從谷歌獲得“me”信息,但我總是在響應代碼401或403中結束。我做錯了什么?這是我的代碼:

private static final String GOOGLE_AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/plus.me";

I get the oauth token by (note...code below is shortened):

我通過(注意…)下面的代碼是縮短):

Account googleAccount = (AccountManager) getSystemService(ACCOUNT_SERVICE).getAccountsByType("com.google")[0];
final Bundle bundle = manager.getAuthToken(googleAccount, GOOGLE_AUTH_TOKEN_TYPE, true, null, null).getResult();
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

So far so good... I now have a token so everything looks good here.

到目前為止一切順利……現在我有了一個標記,所以這里的一切看起來都很好。

Now get the me info:

現在來看看我的信息:

String GOOGLE_ME_URL = "https://www.googleapis.com/plus/v1/people/me";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet request = new HttpGet(GOOGLE_ME_URL);
request.addHeader("Authorization", "OAuth=" + authToken);
final HttpResponse response = client.execute(request);

This gives response code 401.

這給出了響應代碼401。

I have also tried:

我也試過:

final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet request = new HttpGet(GOOGLE_ME_URL + "?access_token=" + authToken);
final HttpResponse response = client.execute(request);

This gives response code 403 - Something like "Daily limit exceeded. Please sign up".

這就給出了響應代碼403——類似於“超出了每天的限制”。請報名”。

What am I doing wrong? what have I missed? How should this be done?

我做錯了什么?我錯過了什么?這應該怎么做呢?

Thanks

謝謝

// Edits below Some more investigation: I added a project into code.google.com/apis/console and took the key generated from there and put into the url, like: https://www.googleapis.com/plus/v1/people/me?key=my_generated_key&access_token=" + authToken. Now the call works fine and I get a 200 response with the correct info. But I really don´t want to use this method if I don´t have to and according to google I should not need to "•If the request requires authorization (such as a request for an individual's private data), then it must include an OAuth 2.0 token. It may also include the API key, but it doesn't have to." - from developers.google.com/+/api/oauth.

//在一些更深入的調查之下:我將一個項目添加到code.google.com/apis/console中,並將其中的關鍵字從那里生成並輸入到url中,比如:https://www.googleapis.com/plus/v1/people/me?關鍵= my_generated_key&access_token = " + authToken。現在通話效果很好,我得到了200個正確的信息。´但我真的不希望使用這種方法´如果我不必須根據谷歌我應該不需要“•如果請求需要授權(如個人的私有數據的請求),那么它必須包括一個OAuth 2.0令牌。它也可能包含API鍵,但它不需要。——從developers.google.com/ + / api / oauth。

Another thing: If I try another url like "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + authToken it works fine.

還有一件事:如果我嘗試另一個url,比如“https://www.googleapis.com/oauth2/v1/tokeninfo?”access_token=" + authToken它運行良好。

6 个解决方案

#1


13  

The issue is regarding the simple api key passed into the request.

問題是關於傳遞到請求中的簡單的api密鑰。

If the key parameter isn't included in the request, or if the Google+ API wasn't activated for that project, you'll get the error: "Daily limit exceeded. Please sign up".

如果在請求中沒有包含關鍵參數,或者如果該項目沒有激活谷歌+ API,您將會得到錯誤:“超出了每日限制。”請報名”。

To solve this problem, you need to do the following:

要解決這個問題,你需要做以下工作:

  • Visit the Google API Console here: https://code.google.com/apis/console/?api=plus
  • 訪問這里的谷歌API控制台:https://code.google.com/apis/console/?
  • Under the Services panel, make sure the Google+ API is turned "on".
  • 在服務面板下,確保谷歌+ API被打開。
  • In the APIs console, click API Access in the left menu.
  • 在API控制台,單擊左側菜單中的API訪問。
  • Copy the API key presented towards the bottom.
  • 復制向底部顯示的API鍵。
  • Include this API key in your HTTP request.
  • 在您的HTTP請求中包含此API密鑰。
GOOGLE_ME_URL + "?access_token=" + authToken + "&key=" + MY_SIMPLE_API_KEY

#2


3  

Most of the newer Google APIs have quotas (like daily usage limits) and some even have billing support (where you get billed per API call). These quotas and billing are calculated per developer's project, and not on a per-end-user basis, so Google needs to know which app to assign your API usage.

大多數更新的谷歌API都有配額(比如日常使用限制),有些甚至有賬單支持(在這里,您可以獲得每個API調用的賬單)。這些配額和計費是根據每個開發人員的項目計算的,而不是基於每個用戶的基礎,因此谷歌需要知道哪個應用程序可以分配您的API使用。

API clients using Google's OAuth 2.0 are typically required to register and get a client ID and client secret.

使用谷歌的OAuth 2.0的API客戶端通常需要注冊並獲取客戶ID和客戶端機密。

This client ID and client secret are returned by the Google APIs console: code.google.com/apis/console.

這個客戶端ID和客戶端秘密由谷歌api控制台返回:code.google.com/apis/console。

You then use these values in your application, and this identifies your app and allows Google to assign your API usage to your developer account/project.

然后在應用程序中使用這些值,這將標識您的應用程序,並允許谷歌將您的API使用分配給開發人員帳戶/項目。

In the AccountManger interface you're using, there is no client ID passed by your app, so Google can't identify which developer account/project's quota to deduct for usage. It also doesn't know that the API has been properly enabled (TOS accepted, etc) by you as a developer. That's why it's asking you to "please sign up" and saying the "Daily limit exceeded" (as the unregistered limit is zero requests for many APIs).

在您使用的AccountManger接口中,您的應用程序沒有通過客戶端ID,因此谷歌不能識別哪個開發人員帳戶/項目的配額可以用來扣除使用。它也不知道您作為開發人員已經正確啟用了API (to accept, etc)。這就是為什么它要求你“請注冊”,並說“每日限制超過”(因為未注冊的限制是許多api的零請求)。

In this scenario, it is necessary for you to pass the "key" value as you did in order to access APIs with OAuth 2.0 tokens retrieved from the AccountManager.

在此場景中,您需要傳遞“key”值,以便訪問從AccountManager檢索到的OAuth 2.0令牌的api。

#3


1  

Why don't some of you try to go to the Google Console. In this way you will be able to have access to to the tools you need to rectify at least 403 forbidden problems. DMC

為什么有些人不試着去谷歌控制台。通過這種方式,您將能夠訪問所需的工具,以糾正至少403個禁用問題。DMC

#4


0  

You are using http at the moment, but you are actually calling a the site over https. Either use a secure connection procedure or use the http:// address.

您現在正在使用http,但實際上您是通過https調用了一個站點。要么使用安全連接過程,要么使用http://地址。

(Sorry, couldn't make it a comment yet)

(抱歉,現在還不能發表評論)

#5


0  

Generate a new auth token and secret for your application and try again.

為您的應用程序生成一個新的auth令牌和秘密,然后再試一次。

That might solve your problem, but your daily retry limit might be exhausted...

這也許能解決你的問題,但你每天的重試極限可能會耗盡……

I had the same issue you're having.

我也有同樣的問題。

#6


0  

I have the same issue you just need to enable google+ api in console enjoy :)

我也有同樣的問題,您只需要在console中啟用谷歌+ api:)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/24/7259ca19d1e73228e4ee5c5a932dd29b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com