Googleは次の手順を実行しFirebaseでゲームAPIを再生使用するには:
***注:getGamesServerAuthCodeは、Googleからのお勧めの方法です。減価償却されますが。一般公開すると、非推奨の注釈を削除するのを忘れている可能性があります。
手順1: ログインに成功したら、認証コードを取得します。たとえば、onSignInSucceeded()を使用できます。 APIクライアントが接続されていることを確認してください。
Games.getGamesServerAuthCode(gameHelper.getApiClient(), [web_client_id]).setResultCallback(new ResultCallback<Games.GetServerAuthCodeResult>() {
@Override
public void onResult(@NonNull Games.GetServerAuthCodeResult result) {
if (result.getStatus().isSuccess()) {
String authCode = result.getCode();
exchangeAuthCodeForToken(authCode);
}
}
});
ステップ2:トークンの認証コードを交換します。
class AuthToken {
String access_token;
String token_type;
int expires_in;
String id_token;
}
void exchangeAuthCodeForToken(final String authCode) {
AsyncTask<Void, Void, AuthToken> task = new AsyncTask<Void, Void, AuthToken>() {
@Override
protected AuthToken doInBackground(Void... voids) {
try {
URL url = new URL("https://www.googleapis.com/oauth2/v4/token");
Map<String, Object> params = new LinkedHashMap<>();
params.put("code", authCode);
params.put("client_id", "[web_client_id]");
params.put("client_secret", "[secret]");
params.put("redirect_uri", "[redirect_uri]");
params.put("grant_type", "authorization_code");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "www.googleapis.com");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
// Build the string
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;) {
sb.append((char) c);
}
// Convert JSON to a Java Object
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
AuthToken token = gson.fromJson(sb.toString(), AuthToken.class);
// Disconnect
conn.disconnect();
return token;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(AuthToken token) {
// Authorize Firebase with Games API
firebaseAuthWithGamesApi(token.id_token);
}
}.execute();
}
ステップ3:認証コードを使用してFirebaseにサインインします。
private void firebaseAuthWithGamesApi(String authToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(authToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
}
}
});
}
問題を再現するための最小限のコードは表示されませんが、なぜ機能していないのかは分かりません。 [MCVE](http://stackoverflow.com/help/mcve)を参照してください。 –