我想使用java客戶端的管理員SDK。我的要求是服務器端應用程序在未經最終用戶明確同意的情況下管理用戶。我遵循以下步驟。Google Admin SDK拋出錯誤的請求java客戶端
我已經創建了一個服務帳號,就是Google API控制檯。 已將服務帳戶添加到Google Apps管理控制檯的第三方oauth訪問部分 爲用戶添加了範圍,user.readonly的範圍相同。 相同的操作,使用該服務的電子郵件驗證
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential=null;
try {
credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("[email protected]")
.setServiceAccountScopes(DirectoryScopes.all())
.setServiceAccountPrivateKeyFromP12File(new File("/Users/xxx/Downloads/file-privatekey.p12"))
.setServiceAccountUser("[email protected]") //Super admin account
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Directory directory = new Directory.Builder(HTTP_TRANSPORT,JSON_FACTORY,credential).setApplicationName("Sync Service").build();
try {
Directory.Users.List list = directory.users().list();
list.setDomain("subdomain.domain.com");
//list.setCustomer("xxx");
Users users = list.execute();
} catch (IOException e) {
e.printStackTrace();
}
我收到的API資源管理器的工作: 創建一個超級管理員到我現在用的是Java客戶端如下用作服務帳戶用戶 跟隨錯誤。不知道爲什麼!
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "access_denied"
}
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:269)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at GappsClient.main(GappsClient.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 0
是的。我添加了xxxxx.apps.googleusercontent.com形式的客戶端ID。我已將以下範圍添加到它。 https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.readonly – user1076371
您是否在API控制檯中使用您的超級用戶創建了服務帳戶管理員帳戶?如果項目是使用超級管理員帳戶創建的,我認爲服務帳戶繼承了超級管理員的權限。 – Emily
哦,是嗎?該服務帳戶是使用不同的域創建的。我正在開發的應用程序是一個多租戶應用程序,它應該可以在多個互不相關的域上工作。在這種情況下,我無法爲我銷售的每個域創建多個服務帳戶。我認爲它的工作方式是,一旦有人想使用這個應用程序,該域的superadmin將會去,並添加我們在他們的域的第三方oauth訪問頁面中提供的服務帳戶。 – user1076371