繁体   English   中英

如何使用Google Play游戏服务实现后端服务器身份验证

[英]how to implement backend server authentication with google play games services for android

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

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

第二步:添加一个buttonclick监听器

第3步:在列表中检查谷歌播放服务的可用性

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 boolean 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;
    }

第四步:

在手机中搜索谷歌帐户:

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);

        }

    }

Step6:ON成功使用后台任务从谷歌获取用户名

的AsyncTask:

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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM