【发布时间】:2020-01-29 13:18:46
【问题描述】:
您好,我正在尝试在我的服务器上使用 Google 的 gmail api 实现发送/接收电子邮件:
private GoogleCredential authorize(HttpTransport httpTransport, JsonFactory jsonFactory ) {
try{
Resource resource = new ClassPathResource("my_key_in_json_format.");
InputStream input = resource.getInputStream();
GoogleCredential credential = GoogleCredential.fromStream(input);
credential.createScoped(GmailScopes.all());
credential.refreshToken();
return credential;
}catch(IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
但是当凭据尝试刷新令牌时,我收到以下异常:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_scope",
"error_description" : "Bad Request"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:394)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:493)
at com.snobo.util.GmailService.authorize(GmailService.java:79)
我尝试将范围参数更改为:
Collection<String> SCOPES = Collections.unmodifiableCollection(Arrays.asList(new String[]{GmailScopes.GMAIL_READONLY}));
刷新令牌时也失败了。 Google 的在线文档对 Java 并不友好。有人遇到类似的问题吗?
【问题讨论】:
-
我不确定这会解决你的问题,但
GoogleCredentialis deprecated 你不应该使用它。检查this library。 -
谢谢。我已将其替换为 ServiceAccountCredentials,但仍无法正常工作。