Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上没有用户登录的情况下,验证有效负载的正确方法是什么?_Android_In App Billing - Fatal编程技术网

在Android上没有用户登录的情况下,验证有效负载的正确方法是什么?

在Android上没有用户登录的情况下,验证有效负载的正确方法是什么?,android,in-app-billing,Android,In App Billing,我已经为订阅服务实施了应用内计费。一切都很好,但我正处于需要确保安全的时刻。我遇到的各种建议都建议通过Plus API使用登录用户的帐户id。然而,如果用户不使用他们的gmail帐户登录,我怎么能得到这个呢?我的想法是生成一个由用户帐户id和sku组合而成的令牌。然后与我的服务器核实购买情况。有没有办法获取用户的帐户id?我想通过一次购买就可以在多台设备上使用该应用程序。如果用户没有使用任何社交api登录,是否有方法跨多个设备验证用户?经过多次尝试、错误和研究,我找到了一个解决方案。因此,对于可

我已经为订阅服务实施了应用内计费。一切都很好,但我正处于需要确保安全的时刻。我遇到的各种建议都建议通过Plus API使用登录用户的帐户id。然而,如果用户不使用他们的gmail帐户登录,我怎么能得到这个呢?我的想法是生成一个由用户帐户id和sku组合而成的令牌。然后与我的服务器核实购买情况。有没有办法获取用户的帐户id?我想通过一次购买就可以在多台设备上使用该应用程序。如果用户没有使用任何社交api登录,是否有方法跨多个设备验证用户?

经过多次尝试、错误和研究,我找到了一个解决方案。因此,对于可能有相同需求/问题的其他人:

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

编译'com.google.android.gms:play services auth:10.2.0'

然后,在需要获取用户帐户id的活动中添加以下内容:

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

}
public类MainActivity扩展了AppCompatActivity{
私人静态最终整数请求\代码\电子邮件=1;
TextView电子邮件,mAcctId;
按钮getID;
字符串accountName;
String TAG=“test”;
专用静态最终整数要求符号要求=55664;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(TextView)findViewById(R.id.email);
mAcctId=(TextView)findViewById(R.id.accountID);
//显示一个弹出窗口,允许用户在存在多个电子邮件时选择电子邮件
试一试{
Intent Intent=AccountPicker.newchooseAccountContent(null,null,
新字符串[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},false,null,null,null,null);
startActivityForResult(意图、请求代码和电子邮件);
}捕获(ActivityNotFounde异常){
//待办事项
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==请求\代码\电子邮件和&resultCode==结果\确定){
accountName=data.getStringExtra(AccountManager.KEY\u ACCOUNT\u NAME);
email.setText(accountName);
//调用异步任务以获取所选电子邮件的accountID
新建RetrieveAccountID().execute(accountName);
}
}
私有类RetrieveAccountID扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串accountName=params[0];
字符串标记=null;
试一试{
token=GoogleAuthUtil.getAccountId(getApplicationContext(),accountName);
}捕获(IOE异常){
Log.e(标记,e.getMessage());
}捕获(UserRecoverableAuthe异常){
startActivityForResult(如getIntent(),需要签名);
}捕获(googleauthe异常){
Log.e(标记,e.getMessage());
}
返回令牌;
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
((TextView)findViewById(R.id.accountID)).setText(“accountID:+s”);
}
}
}
运行将在一个文本视图中为您提供用户选择的电子邮件,并在另一个文本视图中为该电子邮件提供帐户ID。现在可用于为应用程序创建电子邮件用户独有的令牌/密钥。当用户在不同设备上使用应用程序时,这也可用于验证令牌/密钥