【问题标题】:Gmail-API JAVA Client Send Email Insufficient PermissionGmail-API JAVA客户端发送电子邮件权限不足
【发布时间】:2016-02-18 14:57:32
【问题描述】:

我正在尝试通过 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)

:运行失败

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

【问题讨论】:

  • 如果修改范围,请删除您之前保存的令牌/文件夹。
  • 答案如下。

标签: java email oauth-2.0 gmail-api


【解决方案1】:

需要删除 C:\Users\user.credentials 因为我跑了很多次。第一次,我没有授予全部权利。

【讨论】:

    【解决方案2】:

    如果你看看这个答案。它应该回答你的问题。我不能在这里发布答案,因为发布副本是非法的。这对我有用。找了很久才找到这个。让我知道它是否也对您有用。 https://stackoverflow.com/a/39639526/1317559

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        将范围从 SheetsScopes.SPREADSHEETS_READONLY 更改为 SheetsScopes.SPREADSHEETS。

        私有静态最终列表 SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS);

        //private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS_READONLY);
        

        【讨论】:

          猜你喜欢
          • 2016-01-06
          • 2015-10-11
          • 2018-11-08
          • 2018-09-11
          • 1970-01-01
          • 2021-09-11
          • 2022-01-13
          • 2021-05-23
          • 2012-07-07
          相关资源
          最近更新 更多