android - 在 Android 上无需用户登录即可验证有效负载的正确方法是什么?

标签 android in-app-billing

我已经为订阅服务实现了应用内结算。一切都很好,但我正处于需要确保安全的地步。我遇到的各种建议建议通过 Plus API 使用登录用户的帐户 ID。但是,如果用户不使用他们的 gmail 帐户登录,我将如何获得此信息?我的想法是生成一个由用户帐户 ID 和 sku 组合创建的 token 。然后检查我的服务器以验证购买。有没有办法获取用户的帐户ID?我希望通过一次购买就可以在多个设备上使用该应用程序。如果用户没有使用任何社交 API 登录,是否有办法跨多个设备验证用户?

最佳答案

经过反复试验和研究,我找到了解决方案。因此,对于可能有相同需求/问题的任何其他人:

首先,将其添加到您的 build.gradle 文件中:

compile 'com.google.android.gms:play-services-auth:10.2.0'

然后,在需要获取用户帐户 ID 的 Activity 中添加:

    public class MainActivity extends AppCompatActivity{

    private static final int REQUEST_CODE_EMAIL = 1;
    TextView email, mAcctId;
    Button getID;
    String accountName;
    String TAG = "test";
    private static final int REQ_SIGN_IN_REQUIRED = 55664;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        email = (TextView) findViewById(R.id.email);
        mAcctId = (TextView)findViewById(R.id.accountID);
        //Shows a popup allowing user to select email if more than one exists
        try {
            Intent intent = AccountPicker.newChooseAccountIntent(null, null,
                    new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
            startActivityForResult(intent, REQUEST_CODE_EMAIL);
        } catch (ActivityNotFoundException e) {
            // TODO
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
            accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            email.setText(accountName);

            //Call async task to get accountID for selected email
            new RetrieveAccountID().execute(accountName);

        }
    }

    private class RetrieveAccountID extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String accountName = params[0];
            String token = null;
            try {
                token = GoogleAuthUtil.getAccountId(getApplicationContext(), accountName);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (UserRecoverableAuthException e) {
                startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
            } catch (GoogleAuthException e) {
                Log.e(TAG, e.getMessage());
            }
            return token;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            ((TextView) findViewById(R.id.accountID)).setText("AccountID: " + s);
        }
    }

}

运行它会在一个 TextView 中为您提供用户选择的电子邮件,并在另一个 TextView 中为您提供该电子邮件的帐户 ID。现在可以使用它为应用程序创建用户电子邮件独有的 token / key 。当用户在不同设备上使用应用程序时,这也可用于验证 token / key 。

关于android - 在 Android 上无需用户登录即可验证有效负载的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42637172/

相关文章:

android - 为什么要在 IMarketBillingService 之上提供另一个服务?

android WebView 无法正常工作

java - Android 中的 VideoView 无法正确播放视频

android - 整个应用程序中的一个 BillingClient 实例

android - 测试应用内购买管理产品

android - 我怎样才能找出用户的谷歌播放国家?

java - 使用Java在android studio中查询firebase数据库

android - 将小部件 ID 传递给 Activity

android - 如何修复部分安装的 SDK android studio

google-play - 使用 Play 市场封闭 alpha 测试时应用程序没有可用错误