授权给谷歌客户端

授权给谷歌客户端

本文介绍了授权给谷歌客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个类来创建对 BigQuery 和 Google Cloud Storage 的授权.

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

过去我使用了 CredentialStore,它已被弃用.我正在尝试使用 DataStoreFactory,但我发现它只允许我使用 StoredCredential 而我需要 Credential.

In the past I have used CredentialStore which has been deprecated. I am trying to use DataStoreFactory but I discovered that it allows me to use only StoredCredential while I need a Credential.

我知道可以从 Credential 转换为 StoredCredential 但我不确定如何将它们转换为相反的方向(StoredCredential 到 ).例如,我正在创建这样的连接:

I know one can convert from Credential to StoredCredential but I am not sure how to convert them in the opposite direction (StoredCredential to Credential). For example I am creating my connection like this:

Storage.Builder(HttpTransport transport,
    JsonFactory jsonFactory,
    HttpRequestInitializer httpRequestInitializer);


谁能指出我如何实现这一目标的方向?


Could anyone point me in a direction about how to achieve this?

谢谢!

推荐答案

在大多数情况下,无论您在哪里使用 Credential,都可以使用 StoredCredential.只有一点可以使用 Credential,即在 OAuth 回调期间检索访问令牌.从那里 Credential 可以转换为 StoreCredential 并存储到 DataStore.之后,所有的存储和检索都使用 StoredCredential.

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.

但是有些地方是 StoredCredential 不能使用.我刚刚遇到了一个尝试创建 Google Drive API 服务包装器的问题.

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

有一种方法可以使用 GoogleCredential 对象来解决这个问题,它可以根据以下答案从 StoredCredential 创建:
来自 Google API 的存储凭据可使用 Java 重复使用

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

这篇关于授权给谷歌客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:14