微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Apache HttpClient调用Google auth API

我想知道是否可以使用Apache HttpClient调用需要身份validation的Google Api,例如Google Calendar API,而不需要Google代码或库。 (并需要代码来做到这一点)

这是我到目前为止的代码,它得到一个authentication错误,你用什么用户/密码

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);

错误

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

通过Google日历API查询时,执行请求失败

你不使用登录密码,你需要进行身份验证,一旦你是一个访问令牌,那么你可以调用这样的事情。

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/的Auth流程的步骤,您需要在Google Developer控制台上创建一个Oauth2凭据才能启动。 然后它只是要求用户访问他们的数据和请求访问的权限。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐