Requested scopes not allowed

痞子三分冷 提交于 2019-12-25 04:34:24

问题


I'm trying to fetch a user from Google Directory API with the following request:

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();

But I'm getting the following error from the 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"
}

I've checked the scopes are correct, anyone know what's the problem here?


回答1:


Try using one scope or the other. If you only need read access, use the readonly scope. If you need read/write access to users, use the other.

Also, ensure that you've granted the service account's client_id access to these scopes in the Admin's control panel (admin.google.com) and that your ServiceAccountUser is a super admin in the domain.




回答2:


Argh! I removed the old service account and created a new one. After that things started to roll. Guessing I had a wrong p12 file etc. Can finally breathe again, phew.



来源:https://stackoverflow.com/questions/20544188/requested-scopes-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!