Authorization to Google Client

前端 未结 2 1611
遥遥无期
遥遥无期 2020-12-11 08:13

I am writing a class for creating authorization to BigQuery and Google Cloud Storage.

In the past I have used CredentialStore which has been depreca

2条回答
  •  春和景丽
    2020-12-11 08:58

    In most cases, wherever you use Credential, you could use StoredCredential. There is only one point you would work with Credential, which is retrieving the Access Token during the OAuth callback. From there the Credential can be converted to StoreCredential and stored into the DataStore. After that storage and retrieval all works with StoredCredential.

    But there are places were StoredCredential can't be used. I just encountered one trying to create the Google Drive API Service wrapper.

    There is a way to get around this with the GoogleCredential object, it can be created from StoredCredential as per this answer:
    Stored Credential from Google API to be reused using Java

    import com.google.api.client.auth.oauth2.StoredCredential;
    
    public static GoogleCredential createGoogleCredential(StoredCredential storedCredential) {
        GoogleCredential googleCredential = new GoogleCredential.Builder()
            .setTransport(new NetHttpTransport())
            .setJsonFactory(new JacksonFactory())
            .setClientSecrets("client_id", "client_secret")
            .setAccessToken(storedCredential.getAccessToken())
            .build();
    
        return googleCredential;
    }
    

提交回复
热议问题