Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gmail API JAVA客户端发送电子邮件权限不足_Java_Email_Oauth 2.0_Gmail Api - Fatal编程技术网

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

Gmail API JAVA客户端发送电子邮件权限不足,java,email,oauth-2.0,gmail-api,Java,Email,Oauth 2.0,Gmail Api,我正在尝试通过Gmail发送电子邮件。我几乎同意这项服务的所有范围。但我仍然收到错误消息,权限不足 Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Insufficient P

我正在尝试通过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;
}
私有静态最终列表范围=
Arrays.asList(GmailScopes.GMAIL_标签、GmailScopes.GMAIL_组合、GmailScopes.GMAIL_插入、GmailScopes.GMAIL_修改、GmailScopes.GMAIL_只读、GmailScopes.MAIL_谷歌_COM);
静止的{
试一试{
HTTP_TRANSPORT=GoogleNetHttpTransport.newTrustedTransport();
DATA\u STORE\u FACTORY=新文件datastorefactory(DATA\u STORE\u DIR);
}捕获(可丢弃的t){
t、 printStackTrace();
系统出口(1);
}
}
/**
*创建授权凭据对象。
*@返回授权凭证对象。
*@抛出异常
*/
公共静态凭据授权()引发IOException{
//加载客户端机密。
输入流输入=
GmailQuickstart.class.getResourceAsStream(“/client_secret.json”);
谷歌客户机密=
load(JSON_工厂,新的InputStreamReader(in));
//生成流并触发用户授权请求。
GoogleAuthorizationCodeFlow=
新的GoogleAuthorizationCodeFlow.Builder(
HTTP_传输、JSON_工厂、客户端机密、作用域)
.setDataStoreFactory(数据存储工厂)
.setAccessType(“脱机”)
.build();
凭证凭证=新授权代码已安装App(
流,新的LocalServerReceiver())。授权(“用户”);
System.out.println(
“凭证保存到”+数据存储\目录getAbsolutePath());
返回凭证;
}

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

如果你看看这个答案。它应该能回答你的问题。我不能在这里发布答案,因为发布副本是违法的。这对我有用。经过长时间的搜索才找到这个。让我知道这对你是否也有效

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

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

将范围从SheetsScopes.SPREADSHEETS\u只读更改为SheetsScopes.SPREADSHEETS

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

//私有静态最终列表范围=Arrays.asList(SheetsScopes.SPREADSHEETS\u只读);

如果修改作用域,请删除以前保存的令牌/文件夹。这是下面的答案。
//private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS_READONLY);