2016-08-11 22 views
0

我有一個統一的應用程序,並使用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" 
} 

有什麼事我不知道?幫助是非常讚賞...

回答

0

我發現Unity插件裏面根本原因的討論「玩遊戲的服務」在GitHub上: https://github.com/playgameservices/play-games-plugin-for-unity/issues/1293https://github.com/playgameservices/play-games-plugin-for-unity/issues/1309

看來,谷歌的切換認證流程。在給定的鏈接中,他們正在討論在插件內添加電子郵件範圍以再次獲取idtoken。我會在接下來的幾天嘗試,分享我的經驗。

這是會發生什麼一個很好的解釋: http://android-developers.blogspot.de/2016/01/play-games-permissions-are-changing-in.html

如果你做了什麼paulsalameh這裏說(Link to Github),它會重新工作:

paulsalameh:當然。導入unitypackage後,從我的提交(#1295 &#1296)下載NativeClient.cs和 PlayGamesClientConfig.cs,並將它們替換爲 ,並將它們放在正確的位置。

AFTE,你將能夠增加AddOauthScope(「郵件」),以PlayGamesClientConfiguration,它允許你的服務器再次獲取idtoken與serverAuthCode是「團結播放服務,插件」代碼更新...

代碼從Unity片段:

PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder() 
      .AddOauthScope("email") 
      .AddOauthScope("profile") 
      .Build(); 

現在我回到業務:

{ 
    "access_token" : "ya29.Ci8..7kBR-eBdPw1-P7Pe8QUC7e_Zv7qxCHA", 
    "expires_in" : 3600, 
    "id_token" : "eyJhbGciOi......I1NiE0v6kqw", 
    "refresh_token" : "1/HlSZOo......dQV1y4E", 
    "token_type" : "Bearer" 
}