简体   繁体   中英

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

so any help would be very appreciated :)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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