Gmail-API JAVA客户端发送电子邮件权限不足

时间:2021-05-09 15:20:21

I am trying to send email by Gmail. I nearly grant all scope to the service. But I still got the error message, insufficient permission.

我正在尝试通过Gmail发送电子邮件。我几乎将所有范围都授予服务。但我仍然收到错误消息,权限不足。

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}

    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.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:1056)
    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 GmailQuickstart.main(GmailQuickstart.java:133)

:run FAILED

:运行FAILED

private static final List<String> SCOPES =
    Arrays.asList(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
        GmailQuickstart.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

3 个解决方案

#1


3  

Need to delete C:\Users\user.credentials Because I run for many times. For first times, I did not grant the full right.

需要删除C:\ Users \ user.credentials因为我运行了很多次。第一次,我没有给予充分的权利。

#2


0  

If you take a look at this answer. It should answer your question. I cannot post the answer here because it is illegal to post a duplicate. This worked for me. Found this after a long time of searching. Let me know if it worked for you too. https://*.com/a/39639526/1317559

如果你看一下这个答案。它应该回答你的问题。我不能在这里发布答案,因为发布复制品是违法的。这对我有用。经过长时间的搜索后发现了这一点。让我知道它是否适合你。 https://*.com/a/39639526/1317559

#3


0  

Delete C:\Users\user.credentials and change your scopes according to what you need.

删除C:\ Users \ user.credentials并根据需要更改范围。

SCOPES = Arrays.asList(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_INSERT, GmailScopes.MAIL_GOOGLE_COM);

#1


3  

Need to delete C:\Users\user.credentials Because I run for many times. For first times, I did not grant the full right.

需要删除C:\ Users \ user.credentials因为我运行了很多次。第一次,我没有给予充分的权利。

#2


0  

If you take a look at this answer. It should answer your question. I cannot post the answer here because it is illegal to post a duplicate. This worked for me. Found this after a long time of searching. Let me know if it worked for you too. https://*.com/a/39639526/1317559

如果你看一下这个答案。它应该回答你的问题。我不能在这里发布答案,因为发布复制品是违法的。这对我有用。经过长时间的搜索后发现了这一点。让我知道它是否适合你。 https://*.com/a/39639526/1317559

#3


0  

Delete C:\Users\user.credentials and change your scopes according to what you need.

删除C:\ Users \ user.credentials并根据需要更改范围。

SCOPES = Arrays.asList(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_INSERT, GmailScopes.MAIL_GOOGLE_COM);