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();
}
}
错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}