如何调用https://www.googleapis.com/plus/v1/people/me在谷歌 [英] How to call https://www.googleapis.com/plus/v1/people/me at google

查看:1129
本文介绍了如何调用https://www.googleapis.com/plus/v1/people/me在谷歌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,并需要从谷歌的我的信息,但我总是结束了在任何响应code 401或403。我在做什么错了? 这是我的code:

 私有静态最后弦乐GOOGLE_AUTH_TOKEN_TYPE =oauth2如下:https://www.googleapis.com/auth/plus.me;
 

我得到了OAuth的标记(注意...跌破code为缩短):

 帐户googleAccount =(的AccountManager)getSystemService(ACCOUNT_SERVICE).getAccountsByType(com.google)[0];
最后的捆绑包= manager.getAuthToken(googleAccount,GOOGLE_AUTH_TOKEN_TYPE,真实,NULL,NULL).getResult();
字符串的authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
 

到目前为止好......我现在有一个令牌所以一切看起来不错这里。

现在让我信息:

 字符串GOOGLE_ME_URL =htt​​ps://www.googleapis.com/plus/v1/people/me;
最后DefaultHttpClient客户端=新DefaultHttpClient();
最后HTTPGET请求=新HTTPGET(GOOGLE_ME_URL);
request.addHeader(授权,的OAuth =+的authToken);
最后的Htt presponse响应= client.execute(要求);
 

这给了响应code 401。

我也曾尝试:

 最后DefaultHttpClient客户端=新DefaultHttpClient();
(?access_token =GOOGLE_ME_URL + +的authToken)最终HTTPGET请求=新HTTPGET;
最后的Htt presponse响应= client.execute(要求);
 

这给了响应code 403 - 喜欢的东西。超过请注册涨停

我是什么做错了吗?你有什么我错过了什么?应如何做?

感谢

下面//编辑 一些更多的调查: 我添加了一个项目到code.google.com /原料药/控制台,并采取从那里的密钥生成和投入的网址,如: <一href="https://www.googleapis.com/plus/v1/people/me?key=my_generated_key&access_token=">https://www.googleapis.com/plus/v1/people/me?key=my_generated_key&access_token=" +的authToken。 现在,呼叫工作正常,我得到一个200响应的正确的信息。但我真的鸵鸟政策想,如果我不有,并根据谷歌我不应该需要•如果请求需要授权(如个人的私人数据的请求),那么它必须包含使用此方法一个的OAuth 2.0令牌。它也可以包括API密钥,但它不必。 - 从developers.google.com / + / API / OAuth的

另一件事: 如果我尝试另一个链接像 https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=+的authToken 它工作正常。

解决方案

是关于传入请求的简单API密钥的问题

如果关键参数没有包含在请求,或者在Google+的API不激活该项目,你会得到错误:  每日超限,请报名参加。

要解决这个问题,你需要做到以下几点:

  • 访问的谷歌API控制台的位置:<一href="https://$c$c.google.com/apis/console/?api=plus">https://$c$c.google.com/apis/console/?api=plus
  • 在服务面板,确保了Google+的API是接通。
  • 在API控制台,点击左侧菜单中的API访问权限。
  • 复制psented向底部的API密钥$ P $。
  • 包含在HTTP请求该API密钥。

  GOOGLE_ME_URL + +的authToken +access_token =?&放大器;关键=+ MY_SIMPLE_API_KEY
 

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:

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.

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".

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.

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

解决方案

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

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".

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

  • Visit the Google API Console here: https://code.google.com/apis/console/?api=plus
  • Under the Services panel, make sure the Google+ API is turned "on".
  • In the APIs console, click API Access in the left menu.
  • Copy the API key presented towards the bottom.
  • Include this API key in your HTTP request.

GOOGLE_ME_URL + "?access_token=" + authToken + "&key=" + MY_SIMPLE_API_KEY

这篇关于如何调用https://www.googleapis.com/plus/v1/people/me在谷歌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆