本当に使いたいのはサーバー認証コードです。フローは次のとおりです。
ユーザーは通常どおりゲームでゲームにサインインします。 クライアント接続を構築するときは、ServerAuthCodeを要求してください。これは、 です。
String webclientId = getString(R.string.webclient_id);
// Request authCode so we can send the code to the server.
GoogleSignInOptions options = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestServerAuthCode(webclientId)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Games.API)
.addApi(Auth.GOOGLE_SIGN_IN_API, options)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
サインインが完了すると、認証コードを取得し、サーバーに認証コードを送信します。
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == RC_SIGN_IN) {
Log.d(TAG, "onActivityResult RC_SIGN_IN, responseCode="
+ responseCode + ", intent=" + intent);
GoogleSignInResult result =
Auth.GoogleSignInApi.getSignInResultFromIntent(intent);
if (result.isSuccess()) {
sendToServer(result.getSignInAccount().getServerAuthCode(););
} else {
showSignInError(result.getStatus().getStatusCode());
}
サーバーで、そのユーザーのアクセストークンの認証コードを交換します。
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
HTTPTransport,
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
"")
.execute();
その後、サーバーは安全にユーザーのID を取得するためにアクセストークンを検証することができます。
ApplicationVerifyResponse resp = gamesAPI.applications().verify
(applicationId).execute();
Log.d(TAG, "The player id is " + resp.getPlayerId());
のGitHubでこれを行う方法を示すサンプルがあります:https://github.com/playgameservices/clientserverskeleton
は、