使用 Java 中的 Google Plus API 进行 OAuth 2.0 访问 [英] OAuth 2.0 access with Google Plus API in java

查看:25
本文介绍了使用 Java 中的 Google Plus API 进行 OAuth 2.0 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Google Plus API 编写 Web 应用程序,我需要使用 java 设置 OAuth 访问,我搜索了很多并找到了 google java starter 和其他示例,它们非常令人困惑,我不能弄清楚我应该写什么代码来获取令牌我希望如果有人可以告诉我如何通过直接的步骤使用 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

所以任何帮助将不胜感激:)

so any help would be very appreciated :)

推荐答案

最新的Google+ Java快速入门 非常简单,也许您在搜索时发现了一个较旧的项目?此外,有关使用 Java 开始使用 Google+ 的文档 应该有助于您前进.

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.

以下代码段显示了在使用 混合客户端/服务器流程:

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

我正在删除执行 为了简单起见,本主题中讨论的必要检查.

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

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

如果您想以不同的方式执行此操作,您可以在构建 Plus 服务对象之前,根据访问令牌、刷新令牌等显式构建 tokenData 对象.

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 中的 Google Plus API 进行 OAuth 2.0 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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