Reputation: 1473
So my main application is written in Ruby/Rails and that is where the preliminary oauth2 action is happening. Currently I store the email and refresh token from that initial interaction, and now want to use a Scala script to retrieve data from the api in the background using the users credentials. (the point of an API no..?)
After following many examples from the google java api client example page:https://github.com/google/google-api-java-client-samples, I have found that they want me to re-authenticate users via opening a new tab and physically log in. Because I've already authenticated them, is there a way for me to simply continually retrieve data without requiring them to log in again, as this script should be running in the background?
Upvotes: 2
Views: 1073
Reputation: 1086
To refresh an access token using the java-api-client
library:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
val credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.build()
credential.setRefreshToken(refreshToken) // get token from DB or wherever you have persisted it
credential.refreshToken()
credential.getAccessToken // returns new, refreshed access token
Upvotes: 3