gpt4 book ai didi

java - 授权给谷歌客户端

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:36 25 4
gpt4 key购买 nike

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

过去我使用过CredentialStore,但已弃用。我正在尝试使用 DataStoreFactory,但我发现它允许我在需要 Credential 时仅使用 StoredCredential

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

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


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

谢谢!

最佳答案

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

但是有些地方StoredCredential不能使用。我刚刚遇到一个试图创建 Google Drive API 服务包装器的人。

有一种方法可以使用 GoogleCredential 对象来解决这个问题,它可以根据这个答案从 StoredCredential 创建:
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;
}

关于java - 授权给谷歌客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com