![](/img/trans.png)
[英]How to migrate from GoogleCredential to GoogleCredentials and still get access to People API?
[英]GoogleCredential Missing Access Token
我正在使用服務帳戶模型和Google的Java API來檢索和修改用戶。
我能夠使用類似於Google示例的代碼成功創建GoogleCredential對象:
GoogleCredential googleCredential = new GoogleCredentialBuilder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountUser(SERVICE_ACCOUNT_USER)
.setServiceAccountPrivateKeyFromP12File(P12_PRIVATE_KEY_FILE)
.setServiceAccountScopes(Collections.singleton(GLOBAL_USER_AND_ALIAS_SCOPE)
.build();
我在任何示例中都沒有提到必須顯式創建訪問令牌,因此我一直假設上述代碼可以解決這個問題。 真的嗎?
之后,我成功創建了Directory的實例,然后嘗試檢索特定用戶:
User user = new User();
user = directory.users().get(uid).execute();
失敗,拋出NullPointerException。
當我在獲取用戶對象的調用之前檢查GoogleCredential對象時,它似乎不包含訪問令牌:
accessToken = null
refreshToken = null
我想念什么?
如何使用服務帳戶模型獲取訪問令牌?
提前致謝。
您從哪里獲得accessToken? 嘗試
credential.refreshToken();
accessToken = credential.getAccessToken();
另外,您應該考慮在Oauth2 Playground中運行您的憑據。 如果它在操場上工作,那么您的實現可能有問題。
安迪是正確的。 Google Java API的示例省略了這一關鍵步驟。 在運行時,Google代碼會拋出NullPointerException異常,而沒有其他任何細節可以識別發生的位置。 逐步通過Eclipse中的調試器可以清楚地知道,令牌在GoogleCredential中為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.