Invalid delegate for mailbox in Gmail API

Amit Rai :

I am trying to get the Delegates for a mailbox using Gmail API. My application is running on Google App-engine and has the feature of Add, Remove,Get Delegates using Email Setting API. Now I am planning to migrate these features to Gmail API since Email setting API will be deprecated.

Technology wise I am using Java language. I have followed all the steps provided by Gmail API documentation. Authentication to Gmail API is successful. But When I am trying to get the delegates it's giving following error-

404 Not Found { "code" : 404, "errors" : [ { "domain" : "global", "message" : "Invalid delegate", "reason" : "notFound" } ], "message" : "Invalid delegate" }

And inside console below is the error -

*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 $1.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. access$201(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)*

You can observe GmailService line number 106 have an issue. Now I am going to Post my java code.

1: - Authentication Code -

  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:- Next trying to get the Delegates for the mailbox using service object-

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;
    }

Please help me on this since I am still running this in my localhost machine.

droidBomb :

If you want to access other users email account, you must turn on mail delagation that allows the delegate to read, send, and delete on their behalf:

  1. Sign in to your Google Admin console.

    Note: Sign in using your administrator account (does not end in @gmail.com).

  2. From the Admin console Home page, go to Apps>Gsuite>Gmail> User Setting.

  3. Next to Mail delegation, select Let users delegate access to their mailbox to other users in the domain.
  4. (Optional) Select each organization containing users you want to enable mail delegation for and check the Mail Delegation box.
  5. Click Save.

Once enabled, any user who wants to assign a delegate to access their email must set up on mail delegation in their Gmail account.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103634&siteId=1