Search code examples
androidgoogle-apigoogle-oauthgmail-api

Insufficient Permission when using GMAIL AutoForwarding create


String[] googleSCOPES = {GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM};

            GoogleAccountCredential googleAccountCredential = GoogleAccountCredential.usingOAuth2(
                    context, Arrays.asList(googleSCOPES))
                    .setBackOff(new ExponentialBackOff());
            googleAccountCredential.setSelectedAccountName(appSettings.retrieve(Params.GMAIL_ACCOUNT_NAME));
            com.google.api.services.gmail.Gmail mService = new com.google.api.services.gmail.Gmail.Builder(
                    AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), googleAccountCredential)
                    .setApplicationName("StaffConnect")
                    .build();
            ForwardingAddress address = new ForwardingAddress()
                    .setForwardingEmail(emailAddress);
            ForwardingAddress createAddressResult = null;
            try {
                createAddressResult = mService.users().settings().forwardingAddresses()
                        .create("me", address).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (createAddressResult.getVerificationStatus().equals("accepted")) {
                    AutoForwarding autoForwarding = new AutoForwarding()
                            .setEnabled(true)
                            .setEmailAddress(address.getForwardingEmail())
                            .setDisposition("trash");
                    try {
                        autoForwarding = mService.users().settings().updateAutoForwarding("me", autoForwarding).execute();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
}

Error:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}

Solution

  • "insufficientPermissions"

    Means that the current authenticated user does not have the permissions to preform the action you are requesting to use via your application. Permissions are granted at authentication time and are called scopes.

    Users.settings.forwardingAddresses: create request requires authorization with the following scope

    Scope

    https://www.googleapis.com/auth/gmail.settings.sharing

    You need to reauthecate your user and request an additional scope.