在Java谷歌加API的OAuth 2.0访问 [英] OAuth 2.0 access with Google Plus API in java

查看:311
本文介绍了在Java谷歌加API的OAuth 2.0访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图编写使用谷歌加API的Web应用程序,我需要建立与Java OAuth访问,我搜索了很多,发现谷歌的Java起动机和其他的例子,他们都非常混乱,我不能找出了code,我应该写获得令牌
我希望如果有一个人谁可以告诉我怎么去与直线前进的步骤Java中的OAuth访问,我看到stackoverflow.com其他问题,但他们不是非常有帮助,我

I've been trying to write a web application using Google Plus API and i need to set up OAuth access with java , I searched a lot and found google java starter and other examples and they were very confusing, I can't figure out what the code that I should write to get the token I hope if there is someone who can tell me how to get the OAuth access with java in straight forward steps, I saw other questions on stackoverflow.com but they weren't very helpful for me

所以任何帮助将是非常美联社preciated:)

so any help would be very appreciated :)

推荐答案

最新 Google+上的Java快速入门是pretty简单,也许你搜索时发现了一个旧的项目?此外,对于在Google+上的Java入门的文档应该有助于让你去。

The latest Google+ Java Quickstart is pretty straightforward, perhaps you found an older project when searching? Also, the documentation for getting started on Google+ with Java should help to get you going.

下面的片断显示了相关的code使用时访问令牌交换授权code <一个href=\"http://gusclass.com/blog/2013/03/22/using-the-hybrid-clientserver-flow-with-google-sign-in/\">the混合客户端/服务器流量:

The following snippet shows you the relevant code for exchanging the authorization code for an access token when using the hybrid client/server flow:

      GoogleTokenResponse tokenResponse =
          new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY,
              CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute();
      // Create a credential representation of the token data.
      GoogleCredential credential = new GoogleCredential.Builder()
          .setJsonFactory(JSON_FACTORY)
          .setTransport(TRANSPORT)
          .setClientSecrets(CLIENT_ID, CLIENT_SECRET).build()
          .setFromTokenResponse(tokenResponse);

我卸下执行行<一个href=\"http://stackoverflow.com/questions/15415442/authenticating-g-users-on-the-server-side-after-client-side-login/15432006#15432006\">the必要的检查,在简单此线程讨论。<​​/ P>

      // Store the token in the session for later use.
      request.session().attribute("token", tokenResponse.toString());

值得注意这里,你想,除非用户断开您的应用程序坚持这些凭据。样品是因为在生产环境中使用一个会话,会话可以DB支持和服务器重新启动后会恢复。

It's worth noting here that you want to persist these credentials unless the user disconnects your app. The sample is using a session because in production environments, the session can be DB-backed and will be restored after the server restarts.

在您访问/刷新标记和到期时间,令牌适当地打造为V2的OAuth令牌,然后库将在内部刷新访问凭据。下面code显示这是怎么快速入门通过从用户的会话中检索令牌数据完成,还包括由客户端执行的API调用,证明了服务器的Java客户端工作:

After you have the access / refresh token and expiration time, build the credentials for the OAuth v2 token and then the library will internally refresh the access token appropriately. The following code shows how this is done on the quickstart by retrieving the token data from the user's session and also includes an API call performed by the client, proving the server's Java client is working:

      // Build credential from stored token data.
      GoogleCredential credential = new GoogleCredential.Builder()
          .setJsonFactory(JSON_FACTORY)
          .setTransport(TRANSPORT)
          .setClientSecrets(CLIENT_ID, CLIENT_SECRET).build()
          .setFromTokenResponse(JSON_FACTORY.fromString(
              tokenData, GoogleTokenResponse.class));
      // Create a new authorized API client.
      Plus service = new Plus.Builder(TRANSPORT, JSON_FACTORY, credential)
          .setApplicationName(APPLICATION_NAME)
          .build();
      // Get a list of people that this user has shared with this app.
      PeopleFeed people = service.people().list("me", "visible").execute();

如果你想以不同的方式做到这一点,你可以明确地建立从访问令牌tokenData对象,刷新令牌等,构建Plus服务对象之前。

If you wanted to do this differently, you could explicitly construct the tokenData object from the access token, refresh token, and so on, before constructing the Plus service object.

这篇关于在Java谷歌加API的OAuth 2.0访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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