Gmail API中的邮箱代表无效

时间:2018-09-11 07:29:32

标签: java gmail-api google-api-java-client google-email-settings-api

我正在尝试使用Gmail API获取邮箱的代表。 我的应用程序在Google App引擎上运行,并具有使用电子邮件设置API添加,删除,获取代表的功能。 现在,由于不推荐使用电子邮件设置API,因此我打算将这些功能迁移到Gmail API。

从技术角度来讲,我正在使用Java语言。 我已按照Gmail API documentation提供的所有步骤进行操作。对Gmail API的身份验证成功。但是,当我尝试获取代表时,会出现以下错误-

  找不到

404   {     “代码”:404,     “错误”:[{       “ domain”:“ global”,       “ message”:“无效的代表”,       “ reason”:“ notFound”     }],     “ message”:“无效的代表”   }

下面的控制台内部是错误-

  

* com.google.api.client.googleapis.json.GoogleJsonResponseException:找不到404   {     “代码”:404,     “错误”:[{       “ domain”:“ global”,       “ message”:“无效的代表”,       “ reason”:“ notFound”     }],     “ message”:“无效的代表”   }

     在

  com.google.api.client.googleapis.json.GoogleJsonResponseException.fro   m(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)           在com.aeegle.DAOImpl.EmailDAOImpl.getDelegatesGmail(EmailDAOImpl.java:1   43)           在controllers.DelegateController.getListDelegatesGmail(DelegateControll   er.java:82)           在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处           在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。   Java:62)           在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces   sorImpl.java:43)           在java.lang.reflect.Method.invoke(Method.java:498)           在play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)           在play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)           在play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)           在play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)           在play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)           在play.server.PlayHandler $ NettyInvocation.execute(PlayHandler.java:255)           在玩.Invoker $ Invocation.run(Invoker.java:278)           在play.server.PlayHandler $ NettyInvocation.run(PlayHandler.java:233)           在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:51   1)           在java.util.concurrent.FutureTask.run(FutureTask.java:266)           在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask中。   access $ 201(ScheduledThreadPoolExecutor.java:180)           在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask中。   运行(ScheduledThreadPoolExecutor.java:293)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor。   Java:1149)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor   .java:624)           在java.lang.Thread.run(Thread.java:748)*

您可以看到Gmail服务第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;
    }

请帮助我,因为我仍在本地计算机上运行它。

2 个答案:

答案 0 :(得分:1)

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

  1. 登录 到您的Google Admin console

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

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

  3. 邮件委派旁边,选择让用户将对邮箱的访问权限委派给域中的其他用户
  4. (可选)选择包含要为其启用邮件委派的用户的每个组织,然后选中邮件委派框。
  5. 点击保存

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

答案 1 :(得分:1)

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

看起来您正在尝试获取帐户的所有代表的列表,因此您必须使用service.users().settings().delegates().list()而不是service.users().settings().delegates().get()

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

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

相关问题