gpt4 book ai didi

java - Gmail API : Credentials for Gmail API

转载 作者:行者123 更新时间:2023-11-30 11:05:31 25 4
gpt4 key购买 nike

这是 "Java Quickstart" 中给出的代码Gmail API 教程。这是我需要为应用程序创建凭据的操作:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
.setAccessType("online")
.setApprovalPrompt("auto").build();

String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
.build();
System.out.println("Please open the following URL in your browser then type"
+ " the authorization code:\n" + url);

// Read code entered by user.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = null;
try {
code = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}

// Generate Credential using retrieved code.
GoogleTokenResponse response = null;
try {
response = flow.newTokenRequest(code)
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
} catch (IOException e) {
e.printStackTrace();
}
GoogleCredential credential = new GoogleCredential()
.setFromTokenResponse(response);


我可以做些什么来自动执行上述过程,就像在此处完成的以获取凭据以供进一步使用一样?

以下示例适用于 Google Tasks。

GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));

最佳答案

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(service_account)
.setServiceAccountScopes(
Collections.singleton("https://mail.google.com/"))
// .setServiceAccountPrivateKeyFromP12File(new
// File(certLocation))
.setServiceAccountPrivateKey(serviceAccountPrivateKey)
.setServiceAccountUser(senderid).build();

关于java - Gmail API : Credentials for Gmail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624292/

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