我有一個統一的應用程序,並使用google * .aar版本9.4.0的google-play-games插件。我最近將後端(Google App Engine)從php更改爲java。我的問題如下:在PHP中,serverauthcode用於獲取用戶數據(以JWT格式) - 它工作正常。所以我改成了一個Java servlet,並且自2天以來我失敗以獲得有效的idtoken。我能夠從我的應用程序接收服務器授權碼,並通過GoogleAuthorizationCodeTokenRequest(請參閱代碼段)進行有效的令牌響應。不幸的是,它不包含任何idtoken內容,但不包含有效的auth_token。所以我無法獲得用戶標識來標識用戶。當我調用tokenResponse.parseIdToken();它會因NullPointerException而失敗。Unity/Android ServerAuthCode在後端沒有idToken
servlet代碼(AUTHCODE是我從內部團結玩遊戲,插件發送到我的GAE的serverAuthCode):
// (Receive authCode via HTTPS POST)
// Set path to the Web application client_secret_*.json file you downloaded from the
// Google Developers Console: https://console.developers.google.com/apis/credentials?project=_
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "/mypath/client_secret.json";
// Exchange auth code for access token
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
clientSecrets.getDetails().getTokenUri(),
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
REDIRECT_URI) // Specify the same redirect URI that you use with your web
// app. If you don't have a web version of your app, you can
// specify an empty string.
.execute();
String accessToken = tokenResponse.getAccessToken();
// Get profile info from ID token -> HERE IT THROWS AN EXCEPTION.
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject(); // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
令牌響應樣子(其無效了):
{
"access_token" : "ya29.CjA8A7O96w-vX4OCSPm-GMEPGVIEuRTeOxKy_75z6fbYVSXsdi9Ot3NmxlE-j_t-BI",
"expires_in" : 3596,
"token_type" : "Bearer"
}
在我的PHP GAE中,我總是在這個結構裏面有一個包含我的加密數據的idToken。但它現在不見了?!所以我想我在Java中做了不同的事情,或者我在Google控制檯上創建新的OAuth 2.0客戶端時犯了一個錯誤。
我檢查的accessToken通過手動: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.CjA8A7O96w-vX4OCSPm-GMEPGVIEu-RTeOxKy_75z6fbYVSXsdi9Ot3NmxlE-j_t-BI
{
"issued_to": "48168146---------.apps.googleusercontent.com",
"audience": "48168146---------.apps.googleusercontent.com",
"scope": "https://www.googleapis.com/auth/games_lite",
"expires_in": 879,
"access_type": "offline"
}
有什麼事我不知道?幫助是非常讚賞...