gpt4 book ai didi

java - 来自 Google API 的存储凭据可使用 Java 重复使用

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:04 24 4
gpt4 key购买 nike

我已成功授权我的桌面应用程序。它存储一个名为 StoredCredential 的文件。为了不必在每次运行应用程序时都将 URL 复制到浏览器、接受等步骤,我想使用我已经存储的凭据。

到目前为止我的代码:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
JSON_FACTORY,
CLIENT_ID,
CLIENT_SECRET,
SCOPE)
.setDataStoreFactory(dataStoreFactory)
.setApprovalPrompt("auto").setAccessType("offline").build();
//
System.out.println("flow success");

String url = flow
.newAuthorizationUrl()
.setRedirectUri(REDIRECT_URI)
.build();

System.out.println("Please open the following URL in your browser then "
+ "type the authorization code:");

System.out.println(" " + url);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String code = br.readLine();

GoogleTokenResponse tokenResponse
= flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

GoogleCredential credential = new GoogleCredential
.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.addRefreshListener(new CredentialRefreshListener() {
@Override
public void onTokenResponse(
Credential credential,
TokenResponse tokenResponse) throws IOException {

System.out.println("Token Refreshed Succesfully");
}

@Override
public void onTokenErrorResponse(
Credential credential,
TokenErrorResponse tokenErrorResponse) throws IOException {

System.out.println("ERROR WITH TOKEN WTF?");
}})
.build();

如何读取存储的凭据并绕过命令行提示符?

我在想:

if (stored cred exists) { 
try {
// use
} catch {
// prompt the user
}
}

最佳答案

您可以像这样从存储的凭据创建 GoogleCredential 对象:

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets("client_id", "client_secret")
.build();

credential.setAccessToken("access_token");

关于java - 来自 Google API 的存储凭据可使用 Java 重复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19861178/

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