2014-01-30 48 views
1

已從Google開發者控制檯創建項目並創建了服務帳戶。下載了密鑰存儲區。 我需要使用谷歌管理員sdk來創建/刪除/訪問用戶。 我看到Admin SDK ON API & Auth-> API。由於範圍錯誤而無法獲得授權。訪問被拒絕,Google服務帳戶不允許請求的範圍

樣品Java代碼

public boolean makeConnectionWithGoogleAPI(){ 

try{ 
      List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/admin.directory.user", 
       "https://www.googleapis.com/auth/admin.directory.user.readonly"); 

     HttpTransport httpTransport = new NetHttpTransport(); 
     JacksonFactory jsonFactory = new JacksonFactory(); 

     GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(jsonFactory) 
      .setServiceAccountId(clientEmail) 
      .setServiceAccountUser(userId) 
      .setServiceAccountScopes(scopes) 
      .setServiceAccountPrivateKeyFromP12File(
       new java.io.File(privateKeyStoreLocation)) 
      .build(); 

     Directory admin = 
      new Directory.Builder(httpTransport, jsonFactory, null) 
      .setHttpRequestInitializer(credential).build(); 

     Directory.Users.List list = admin.users().list(); 
     Users users = list.execute(); 
     List<User> listUsers=users.getUsers(); 
     for(User user:listUsers){ 
      System.out.println(user.getId()); 
     } 
     return true; 
}catch(Exception e){ 
     e.printStackTrace(); 
} 
    return false; 
} 

com.google.api.client.auth.oauth2.TokenResponseException:400錯誤的請求 { 「錯誤」: 「ACCESS_DENIED」, 「ERROR_DESCRIPTION」:「請求的範圍不允許:https://www.googleapis.com/auth/admin.directory.userhttps://www.googleapis.com/auth/admin.directory.user.readonly「 } 在com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105) 在com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed( TokenRequest.java:287)

+1

是的,這是從管理控制檯 - >安全 - >高級設置 - >管理第三方客戶端OAuth添加所需的範圍。需要指定服務帳戶clientId與範圍。點擊Authroize。在代碼中使用客戶端電子郵件,並且還需要指定域Directory.Users.List list = admin.users()。list()。setDomain(clientDomain); – user3240209

回答

6

我們遇到了這個問題 - 沒有,Google文檔中的這一點非常明確。

您爲API創建的客戶ID是您在API範圍列表中輸入的客戶名稱。

轉到此處並輸入您的項目。建立您希望訪問的API:https://console.developers.google.com/project

然後在左側的Credentials選項卡上,創建新的OAuth客戶端ID /名稱信息。

你這裏拿客戶端名稱,然後轉到你的Apps管理員安全接口: https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients 或者:Admin.google.com>然後安全>然後選擇高級>然後管理OAuth客戶

帶你收到的CLIENT_ID從Developers Console創建您的項目,並將其指定爲您的客戶名稱,以便訪問API項目上的特定範圍。

對我們來說,無論如何,這是解決我們的拒絕訪問錯誤的方法。

因Google沒有正確記錄此過程而感到羞恥。花了我們4天的時間來弄清楚。我們是一家.net商店,甚至他們的NuGet軟件包也有拼寫錯誤,拼寫錯誤和缺少資源。非常令人沮喪的是,沒有更好地記錄這些步驟。

1

我在提供的鏈接中找不到以下選項。我花了更多的時間在這個。

https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients 或者:Admin.google.com>然後安全>然後選擇高級>然後管理 OAuth用戶端

是否有這方面的谷歌的任何更新,請更新如果任何人有培訓相關信息。 找出我的代碼在下面。

GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId("[email protected]") 
      .setServiceAccountScopes(scopes) 
      .setServiceAccountPrivateKeyFromP12File(new File("D:\\ref\\privatekey.p12")) 
      .setServiceAccountUser("xxxxxxxxxxxxxxxxxxx") 
      .build(); 
Compute compute = new Compute.Builder(
      httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME) 
      .setHttpRequestInitializer(credential).build(); 
Compute.Instances.List instances = compute.instances().list(projectId, zoneName); 
    InstanceList list = instances.execute(); 

該錯誤消息是

403禁止{ 「錯誤」: 「ACCESS_DENIED」, 「ERROR_DESCRIPTION」: 「請求的範圍不允許: https://www.googleapis.com/auth/compute」}

0

我具有相同的問題403 access_denied並通過跟蹤操作進行修復。

如上面所提到的FurnGuy

https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients或者:Admin.google.com>然後安全>然後選擇高級>然後管理OAuth客戶

帶你創建一個從開發者控制檯項目收到的CLIENT_ID和將其指定爲您希望訪問API項目上特定範圍的客戶名稱。

enter image description here

客戶名稱是從開發者控制檯創建您的客戶端ID,並添加範圍爲例https://www.googleapis.com/auth/admin.directory.group,https://www.googleapis.com/auth/admin.directory.orgunit.readonly

您可以通過逗號分隔,然後授權。你可以在你的本地主機上嘗試它:8888

相關問題