使用Apache HttpClient调用Google auth API [英] call Google auth API using Apache HttpClient

查看:179
本文介绍了使用Apache HttpClient调用Google auth API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用Apache HttpClient调用需要身份验证的Google API,例如Google Calendar API,并且不需要Google代码或库。 (并且需要代码来做)

这是我到目前为止的代码,它得到一个认证错误,
你对用户使用什么/ password?

  HttpPost request = new HttpPost(https://www.googleapis.com/calendar/v3/users/我/ calendarList /主键=的myKey)?; 
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider()。setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user,password));
HttpResponse response = client.execute(request);

错误:

 errors:[
{
domain:global,
reason:required,
message:需要登录,
locationType:header,
location:Authorization
}
],
code:401,
message:需要登录


解决方案

t使用您需要认证的登录名和密码,您将拥有访问令牌,然后您可以调用类似这样的内容。

https://www.googleapis.com/calendar/v3/users/me/calendarList/primary? access_token = {tokenFromAuth}



您可以使用任何能够进行HTTP POST和HTTP GET的语言向Google进行身份验证。以下是我对Google http://www.daimto .com / google-3-legged-oauth2-flow / ,您需要在Google Developer Console上创建一个Oauth2凭据才能启动。然后它只是要求用户访问他们的数据和请求访问的权限。

I would like to know if it is possible to call a Google API that requires auth such as the Google Calendar API using the Apache HttpClient, and no Google code or libraries. (and need the code to do it)

This is the code I have so far, its gets an auth error, What do you use for the user/password?

HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);

Error:

"errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"

解决方案

You don't use Login and password you need to be authenticated once you are you will have an access token then you can call something like this.

https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?access_token={tokenFromAuth}

You can authenticate to Google with any language that is capable of a HTTP POST and a HTTP GET. Here is my walk though of the Auth flow to Google http://www.daimto.com/google-3-legged-oauth2-flow/ you will need to create a Oauth2 credentials on Google Developer console to start. Then its just a matter of asking the user for permission to access their data and requesting the access.

这篇关于使用Apache HttpClient调用Google auth API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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