简体   繁体   English

使用Java中的Google Plus API访问OAuth 2.0

[英]OAuth 2.0 access with Google Plus API in java

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 我一直在尝试使用Google Plus API编写Web应用程序,我需要使用java设置OAuth访问权限,我进行了很多搜索,发现google java starter和其他示例,它们非常令人困惑,我无法弄清楚是什么我应该编写以获得令牌的代码,我希望如果有人可以告诉我如何通过直接的步骤来获取Java的OAuth访问权限,我在stackoverflow.com上看到了其他问题,但它们对我不是很有帮助

so any help would be very appreciated :) 所以任何帮助将不胜感激:)

The latest Google+ Java Quickstart is pretty straightforward, perhaps you found an older project when searching? 最新的Google+ Java快速入门非常简单,也许您在搜索时发现了一个较旧的项目? Also, the documentation for getting started on Google+ with Java should help to get you going. 此外, 有关使用Java进行Google+入门文档也应该有助于您入门

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

I'm removing the lines performing the requisite checks discussed in this thread for simplicity. 为了简单起见,我将删除执行此线程中讨论的必要检查的行。

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

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. 该示例正在使用会话,因为在生产环境中,该会话可以由数据库支持,并且在服务器重新启动后将被恢复。

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. 获得访问/刷新令牌和到期时间后,构建OAuth v2令牌的凭据,然后库将在内部适当地刷新访问令牌。 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: 以下代码显示了如何通过从用户会话中检索令牌数据在快速入门上完成此操作,还包括客户端执行的API调用,证明服务器的Java客户端正在运行:

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

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. 如果您希望以其他方式执行此操作,则可以在构造Plus服务对象之前从访问令牌,刷新令牌等显式构造tokenData对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM