我正在尝试使用以下请求从Google Directory API获取用户:
Collection<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/admin.directory.user.readonly", "https://www.googleapis.com/auth/admin.directory.user");
URL url = getClass().getResource("privatekey.p12");
GoogleCredential credential = new GoogleCredential.Builder()
.setJsonFactory(request.getJsonFactory())
.setServiceAccountId("foobar.com")
.setServiceAccountScopes(SCOPES)
.setTransport(new NetHttpTransport())
.setServiceAccountUser("foo@bar.com")
.setServiceAccountPrivateKeyFromP12File(new File(url.getPath())).build();
Directory dir = new Directory.Builder(GoogleNetHttpTransport.newTrustedTransport(), request.getJsonFactory(), credential)
.setApplicationName(request.getApplicationName())
.build();
但是我从API中收到以下错误:
Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "access_denied",
"error_description" : "Requested scopes not allowed: https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly"
}
我检查了范围是否正确,有人知道这里有什么问题吗?
答案 0 :(得分:0)
尝试使用一个范围或另一个范围。如果您只需要读访问权限,请使用只读范围。如果您需要对用户进行读/写访问,请使用另一个。
另外,请确保您已在管理员控制面板(admin.google.com)中授予服务帐户对client范围的访问权限,并确保您的ServiceAccountUser是域中的超级管理员。
答案 1 :(得分:0)
哎呀!我删除了旧的服务帐户并创建了一个新帐户。事情开始后就开始了。猜测我有一个错误的p12文件等。最后可以再次呼吸,phew。