Gmail 中的邮箱委托无效 API

Invalid delegate for mailbox in Gmail API

我正在尝试使用 Gmail API 获取邮箱的代表。 我的应用程序在 Google App-engine 上 运行,具有使用电子邮件设置 API 添加、删除、获取委托的功能。 现在我计划将这些功能迁移到 Gmail API,因为电子邮件设置 API 将被弃用。

技术方面,我使用的是 Java 语言。 我已按照 Gmail API documentation 提供的所有步骤进行操作。 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" : "Invalid delegate" }

at com.google.api.client.googleapis.json.GoogleJsonResponseException.fro m(GoogleJsonResponseException.java:150) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClie ntRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClie ntRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest .interceptResponse(AbstractGoogleClientRequest.java:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest .executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest .executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest .execute(AbstractGoogleClientRequest.java:469) at com.aeegle.services.GmailService.retrieveEmailDelegates(GmailService. java:106) at com.aeegle.DAOImpl.EmailDAOImpl.getDelegatesGmail(EmailDAOImpl.java:1 43) at controllers.DelegateController.getListDelegatesGmail(DelegateControll er.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557) at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508) at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484) at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479) at play.mvc.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. access1(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask. run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor. java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:624) at java.lang.Thread.run(Thread.java:748)*

您可以观察到 GmailService 第 106 行有问题。现在我要 Post 我的 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;
    }

请帮助我解决这个问题,因为我在我的本地主机上仍然是运行这个。

如果您想要访问其他用户的电子邮件帐户,您必须打开允许代理人代表他们阅读、发送和删除的邮件延迟:

  1. 在您的 Google Admin console.

    中签名

    注意:使用您的管理员帐户登录(不以 @gmail.com).

  2. 从管理控制台主页转到Apps>Gsuite>Gmail> 用户设置.

  3. 邮件委托、select 让用户将对其邮箱的访问权委托给域中的其他用户旁边。
  4. (可选)Select 每个包含您要为其启用邮件委派的用户的组织,并选中 邮件委派 框。
  5. 单击保存

启用后,任何想要指定代理访问其电子邮件的用户都必须 set up on mail delegation 在其 Gmail 帐户中。

免责声明:我在 Gmail 和 Gmail API 平台上工作。

您似乎正在尝试获取一个帐户的所有受托人的列表,因此您必须使用service.users().settings().delegates().list() instead of service.users().settings().delegates().get()

请注意,这将需要进一步更改您的代码,以处理返回的 ListDelegatesResponse

顺便说一句,您收到了 "Invalid delegate" 错误,因为 service.users().settings().delegates().get(user, "me") 正在提供 me 作为您尝试检索的代表的电子邮件。您需要提供有效的电子邮件地址才能使请求成功。但是,我意识到这很可能是由于您错误地使用了 Get 委托方法而不是 List 委托方法。