gpt4 book ai didi

java - 如何通过 OAUTH 使用 google 电子邮件审核 API(特别是 createMailMonitor)

转载 作者:行者123 更新时间:2023-12-02 04:33:03 26 4
gpt4 key购买 nike

摘要:我需要帮助获取服务(开发人员控制台中的服务帐户)进行身份验证,以便能够创建邮件监视器(电子邮件审核 API)

我读过的最相关/官方页面是:

developers.google.com/admin-sdk/email-audit/auth

developers.google.com/identity/protocols/OAuth2Developers.google.com/identity/protocols/OAuth2ServiceAccount

我已经阅读了几个示例,但它们要么是旧的(在 oauth 成为强制性之前),要么是用于客户端应用程序的(经过用户同意)。

下面的代码收到错误 401(需要授权),因为它缺少在这种情况下进行身份验证的代码。

HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Arrays.asList("https://apps-apis.google.com/a/feeds/compliance/audit/ https://www.googleapis.com/auth/userinfo.email"))
.setServiceAccountUser(<account user>)
.setServiceAccountPrivateKeyFromP12File(new java.io.File(<PKCS12_FILE_PATH>))
.build();

MailMonitor monitor = new MailMonitor();

Calendar beginDate = Calendar.getInstance();
beginDate.set(2009, Calendar.JUNE, 15, 0, 0);
monitor.setBeginDate(beginDate.getTime());
Calendar endDate = Calendar.getInstance();
endDate.set(2009, Calendar.JUNE, 30, 23, 20);
monitor.setEndDate(endDate.getTime());
monitor.setIncomingEmailMonitorLevel("FULL_MESSAGE");
monitor.setOutgoingEmailMonitorLevel("HEADER_ONLY");
monitor.setDraftMonitorLevel("FULL_MESSAGE");
monitor.setChatMonitorLevel("FULL_MESSAGE");
monitor.setDestUserName(<user name>);

AuditService service_audit = new AuditService(<domain>, <app id>);

//WHAT CODE DO I INSERT HERE TO AUTHORIZE THIS SERVICE?
//I've tried :
//service_audit.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
//but it requires Consumer Secret, etc which are only relevant for web applications where there is no user consent

GenericEntry entry = service_audit.createMailMonitor(<user id to monitor>, monitor);

我见过使用方法生成器的其他 API 的示例,例如:

Reports service = new Reports.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential).build();

所以我也尝试了以下方法,但我得到了一个找不到符号:

AuditService service = new AuditService.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential).build();

我错过了什么?我可以得到一些关于如何离开这里的指示吗?

非常感谢!

最佳答案

找到了答案...也许其他人可以从中受益。

缺少的代码是

credential.refreshToken();
service.setOAuth2Credentials(credential);

关于java - 如何通过 OAUTH 使用 google 电子邮件审核 API(特别是 createMailMonitor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200277/

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