【问题标题】:how to implement backend server authentication with google play games services for android如何使用适用于 android 的 google play 游戏服务实现后端服务器身份验证
【发布时间】:2016-08-19 21:06:55
【问题描述】:

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

【问题讨论】:

    标签: android google-play google-play-games


    【解决方案1】:

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

    Step2 : 添加按钮点击监听器

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

    受保护的布尔值 checkPlayServices() { 整数结果代码 =

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

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

    异步任务:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多