user193116
user193116

Reputation: 3568

How to use Google Drive Client Library for Java with my homegrown OAUTH 2 Framework for web application?

My web application already does OAUTh authentication successfully with Facebook, LinkedIn, Google etc. using REST and/or signpost-oauth library.

So once I already have ACCES_TOKEN from GoogleAPIs server using my web app , I want to use Google Drive Client to access files etc.

This is the google Drive example but it relies upon using Google authorization code flow ( which I can't understand well enough to use with my Java EE web app )

 // authorization
 Credential credential = authorize();
 // set up the global Drive instance
 drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
     .setApplicationName(APPLICATION_NAME).build();

How do I change this to use ACCESS_TOKEN and other credentials that I have already obtained through my web application's OAUTH framework ?

Upvotes: 2

Views: 1290

Answers (1)

Nicolas Garnier
Nicolas Garnier

Reputation: 12374

If you already have an Access Token, you can simply do this:

Credential credential = new GoogleCredential().setAccessToken(yourAccessToken);

If you have an Access Token AND a Refresh Token, which is much better since you can get automatic token refresh from the client library, you should do this:

Credential credential = new GoogleCredential.Builder()
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
    .setJsonFactory(JSON_FACTORY)
    .setTransport(HTTP_TRANSPORT)
    .build()
    .setAccessToken(yourAccessToken)
    .setRefreshToken(yourRefreshToken);

Also Check the GoogleCredential Javadoc for more examples.

Upvotes: 8

Related Questions