我似乎无法利用Oauth的Google。我错过了什么?
错误消息:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_scope",
"error_description" : "Invalid oauth scope or ID token audience provided."
}
Java代码:
私有空printLabels() {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
List<String> scopes = new ArrayList<>();
scopes.add(GmailScopes.GMAIL_LABELS);
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("C:\\test\\credential.json"));
credential.createScoped(scopes);
credential.refreshToken(); // error happens here
String appName = "VS";
Gmail.Builder builder = new Gmail.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(appName);
Gmail gmail = builder.build();
Object o = gmail.users().labels().list("me").execute();
System.out.println("o = " + o);
}
Google配置:
登录到https://console.developers.google.com/
发布于 2021-09-10 07:41:55
credential.createScoped(scopes)
返回具有给定作用域的凭据对象的副本。您可以通过credential.getServiceAccountScopes()
验证这一点,并检查是否设置了范围。
尝试将值赋值给凭据对象,如。
credential = credential.createScoped(scopes);
发布于 2022-11-24 14:48:29
在我的例子中,我遇到了使用Python获取默认凭据的问题。对于加载了gcloud auth application-default login
或Kubernetes工作负载标识的凭据,它工作得很好。但是在GOOGLE_APPLICATION_CREDENTIALS
中使用服务帐户文件时,我在试图调用credentials.refresh()
时遇到了这个问题。我通过将scopes
参数显式地提供给具有default()
作用域的default()
函数来解决这个问题。
所以我改变了主意:
from google.auth import default
credentials, _ = default()
对此:
from google.auth import default
credentials, _ = default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
https://stackoverflow.com/questions/60401040
复制相似问题