我正在尝试使用 Gmail API 获取邮箱的代表。我的应用程序在 Google App-engine 上运行,并具有使用电子邮件设置 API 添加、删除、获取代表的功能。现在我计划将这些功能迁移到 Gmail API,因为电子邮件设置 API 将被弃用。
技术方面,我正在使用 Java 语言。我已按照 Gmail API文档提供的所有步骤进行操作。对 Gmail API 的身份验证成功。但是当我试图让代表时,它给出了以下错误-
404 Not Found { "code" : 404, "errors" : [ { "domain" : "global", "message" : "Invalid delegate", "reason" : "notFound" } ], "message" : "Invalid delegate " }
下面的控制台内部是错误 -
*com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found { "code" : 404, "errors" : [ { "domain" : "global", "message" : "Invalid delegate", "reason " : "notFound" } ], "message" : "无效委托" }
在 com.google.api.client.googleapis.json.GoogleJsonResponseException.from (GoogleJsonResponseException.java:150) 在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClie ntRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClie ntRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest $1.interceptResponse(AbstractGoogleClientRequest.java:321)在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest .executeUnparsed(AbstractGoogleClientRequest.java:419) 在 com.google.api .client.googleapis.services。AbstractGoogleClientRequest .executeUnparsed(AbstractGoogleClientRequest.java:352) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest .execute(AbstractGoogleClientRequest.java:469) 在 com.aeegle.services.GmailService.retrieveEmailDelegates(GmailService.java:106) ActionInvoker.invoke(ActionInvoker.java:161) at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:255) at play.Invoker$Invocation.run(Invoker.java:278) at play.server.PlayHandler$NettyInvocation .run(PlayHandler.java:233) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51 1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util .concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask。在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask 访问 $201(ScheduledThreadPoolExecutor.java:180)。在 java.util.concurrent.ThreadPoolExecutor$Worker 的 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 处运行 (ScheduledThreadPoolExecutor.java:293)。
您可以观察到 GmailService 行号 106 有问题。现在我要发布我的java代码。
1: -验证码 -
public Gmail getGmailService(String email) throws Exception {
System.out.println("-------------getGmailService");
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Collection<String> SCOPES = new ArrayList<String>();
SCOPES.add(GmailScopes.GMAIL_SETTINGS_BASIC);
SCOPES.add(GmailScopes.MAIL_GOOGLE_COM);
SCOPES.add(GmailScopes.GMAIL_MODIFY);
SCOPES.add(GmailScopes.GMAIL_READONLY);
GoogleCredential credential;
// To load classpath resources.
ClassLoader classLoader = getClass().getClassLoader();
new File(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL).setServiceAccountUser(email).setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)).build();
System.out.println("----calling Builder");
service = new Gmail.Builder(httpTransport, jsonFactory, null).setHttpRequestInitializer(credential)
.setApplicationName(APPLICATION_NAME).build();
return service;
}
2:-接下来尝试使用服务对象获取邮箱的代表-
public Delegate retrieveEmailDelegates(String user, Gmail service) throws Exception {
if (isBlankOrNullString(user)) {
throw new IllegalArgumentException();
}
Delegate delegatesResponse=null;
try {
System.out.println("Call retrieveEmailDelegates for "+user);
delegatesResponse = service.users().settings().delegates().get(user, "me").execute();
System.out.println("-------service" + delegatesResponse.getDelegateEmail());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return delegatesResponse;
}
请帮助我,因为我仍在我的 localhost 机器上运行它。