要使用谷歌玩遊戲API和火力地堡請按照下列步驟操作:
***注:getGamesServerAuthCode是從谷歌推薦的方式;儘管這是貶值。在向公衆發佈時,他們可能忘記刪除棄用註釋。
第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)。 –