googleapis / google-auth-library-java

Open source Auth client library for Java
https://developers.google.com/identity
BSD 3-Clause "New" or "Revised" License
405 stars 218 forks source link

Migrating old GoogleCredential object to new GoogleCredentials object for OAuth #1386

Open z0mg opened 2 months ago

z0mg commented 2 months ago

I have a web application and obtain an auth code with which I get a refresh token. I then supply this refresh token to the library and it can obtain a new access code before I access it.

This is how I currently do it for the old version:

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.bigquery.Bigquery;

    public static final HttpTransport httpTransport = new NetHttpTransport();
    public static final JsonFactory jsonFactory = new JacksonFactory();

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setClientSecrets("client_id", "secret")
        .build()
        .setFromTokenResponse(new TokenResponse().setRefreshToken("refresh_token");

    credential.refreshToken();

    Bigquery bigquery = new Bigquery.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("appName").build();

And I would like to build the equivalent with a GoogleCredentials object, which the newer BigQuery library needs - how to do this?

    com.google.auth.oauth2.GoogleCredentials credentials = ??? ;

    import com.google.cloud.bigquery.BigQuery bigquery =
        BigQueryOptions.newBuilder()
            .setCredentials(credentials)
            .setProjectId(projectId)
            .build()
            .getService();
dungthantai-vns commented 2 months ago

Same question

z0mg commented 1 month ago

Any thoughts?

z0mg commented 3 weeks ago

Anyone?