gpt4 book ai didi

android - 如何使用适用于 Android 的 Google Play 游戏服务实现后端服务器身份验证

转载 作者:太空狗 更新时间:2023-10-29 14:49:11 27 4
gpt4 key购买 nike

也许我忽略了什么,但通过 Play 游戏服务文档 https://developers.google.com/games/services/android/quickstart ,我还不知道如何实现后端服务器身份验证,比如如何从谷歌的服务器获取 token 并将其传递到我自己的后端服务器以验证登录。我希望有人能给我一个线索。谢谢!

最佳答案

第 1 步:使用标准的谷歌登录按钮创建一个按钮

第二步:添加一个按钮点击监听器

第 3 步:在监听器中检查 google play 服务的可用性

public void googleLoginClicked(View v){
if (checkPlayServices()) {
pickUserAccount();
}else{
showToast("Google Play Services is not installed or updated in your deivce", Toast.LENGTH_LONG);
}
}

protected bool 值 checkPlayServices() { int 结果代码 =

GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
// Log.i("GCM", "This device is not supported.");
finish();
}
return false;
}
return true;
}

第 4 步:

在手机中搜索谷歌账户:

private void pickUserAccount() {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}

第五步:

onActivityResult:

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
getUsername(null);
} else if (resultCode == RESULT_CANCELED) {
showToast("You must pick an account",
Toast.LENGTH_SHORT);
}

} else if (requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR && resultCode == RESULT_OK) {
Bundle extra = data.getExtras();
String oneTimeToken = extra.getString("authtoken");
getUsername(oneTimeToken);

}

}

第 6 步:使用后台任务成功从 google 获取用户名

异步任务:

doinbackground(){
...
fetchToken
...
}


protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present, which is
// recoverable, so we need to show the user some UI through the activity.
mActivity.handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
}
return null;
}

关于android - 如何使用适用于 Android 的 Google Play 游戏服务实现后端服务器身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com