如何使用android的google play games服务实现后端服务器身份验证
也许我忽略了什么,但通过Play Games Services文档,我还不知道如何实现后端服务器身份验证,比如如何从Google服务器获取令牌并将其传递到我自己的后端服务器以验证登录。我希望有人能给我一个线索。谢谢 步骤1: 使用标准google登录按钮创建一个按钮 步骤2:添加按钮单击侦听器 步骤3:在listner中检查google play服务的可用性如何使用android的google play games服务实现后端服务器身份验证,android,google-play,google-play-games,Android,Google Play,Google Play Games,也许我忽略了什么,但通过Play Games Services文档,我还不知道如何实现后端服务器身份验证,比如如何从Google服务器获取令牌并将其传递到我自己的后端服务器以验证登录。我希望有人能给我一个线索。谢谢 步骤1: 使用标准google登录按钮创建一个按钮 步骤2:添加按钮单击侦听器 步骤3:在listner中检查google play服务的可用性 public void googleLoginClicked(View v){ if (checkPlayServices
public void googleLoginClicked(View v){
if (checkPlayServices()) {
pickUserAccount();
}else{
showToast("Google Play Services is not installed or updated in your deivce", Toast.LENGTH_LONG);
}
}
受保护的布尔值checkPlayServices(){
int resultCode=
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);
}
步骤5:
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;
}