Google api 当我向googleapis提出请求时,收到400个错误请求
我正在尝试获取特定用户的组列表(通讯组列表)。它正在失败 请求是Google api 当我向googleapis提出请求时,收到400个错误请求,google-api,bad-request,google-developers-console,google-groups,google-groups-api,Google Api,Bad Request,Google Developers Console,Google Groups,Google Groups Api,我正在尝试获取特定用户的组列表(通讯组列表)。它正在失败 请求是 `GET /admin/directory/v1/groups HTTP/1.1 Host: www.googleapis.com Content-length: 0 Authorization: Bearer ya29.Il-9B-9Z88RKqK73dr-NyeeJnCz0PqlPrMgxlr0JO6gc2q2jcROxsu7y6EaTHAlRkJatv5l3kd_PIPHrB7wXeMvfe0YTXupzupJ8-PUyrR
`GET /admin/directory/v1/groups HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer ya29.Il-9B-9Z88RKqK73dr-NyeeJnCz0PqlPrMgxlr0JO6gc2q2jcROxsu7y6EaTHAlRkJatv5l3kd_PIPHrB7wXeMvfe0YTXupzupJ8-PUyrRcCIBE_qpPP8V7NoM0JJp`
但是我得到的错误响应如下所示
{
"error": {
"code": 400,
"message": "Bad Request",
"errors": [
{
"domain": "global",
"message": "Bad Request",
"reason": "badRequest"
}
]
}
}
不仅是在获取组时,而且从googleapis.com发出的每个API调用都是这样的。我对一些google API的使用也有同样的错误/困惑 我假设您使用的服务帐户具有域范围的委派访问权限 假设是,问题在于,除了服务帐户凭据之外,您还需要设置一个用户,在发出请求时,google凭据将模拟用户。也就是说,根据我的理解,域范围的委托只允许您像实际用户一样行事 除了直接使用构建器,我看不到任何通过GoogleBaseAPI设置的方法。以下是一个示例:
static GoogleCredential createCredential(HttpTransport传输,JsonFactory JsonFactory)引发异常{
最终字符串credentialJsonPath=System.getProperty(“user.home”)+“/.config/gcloud/service cred from console.json”;
try(InputStream credentialStream=新文件InputStream(credentialJsonPath)){
final GoogleCredential firstPassCred=GoogleCredential.fromStream(credentialStream,transport,jsonFactory);
返回新的GoogleCredential.Builder()
.setTransport(运输)
.setJsonFactory(jsonFactory)
.setServiceAccountId(firstPassCred.getServiceAccountId())
.setServiceAccountProjectId(firstPassCred.getServiceAccountProjectId())
.setServiceAccountPrivateKey(firstPassCred.getServiceAccountPrivateKey())
.setServiceAccountPrivateKeyId(firstPassCred.getServiceAccountPrivateKeyId())
//服务帐户必须代表实际用户并具有作用域
.setServiceAccountScopes(列出.newArrayList(GmailScopes.GMAIL_组合、GmailScopes.GMAIL_设置_共享))
.setServiceAccountUser(“a.real.gsuite。account@example.com")
.build();
}
}
重要的部分是定义ServiceAccountUser
(您模拟的用户)和为该用户假定的ServiceAccountScope
我发了一封信。如果需要等效的REST调用序列,可以通过本地代理(如charles
(et al))运行此操作