在 Android 上无需用户登录即可验证负载的正确方法是什么?
What is proper way to verify payload without user login on Android?
我已经为订阅服务实施了应用内结算。一切都很好,但我正处于需要确保安全的地步。我收到的各种建议 运行 建议通过加号 API 使用登录用户的帐户 ID。但是,如果用户不使用他们的 gmail 帐户登录,我将如何获得此信息?我的想法是生成一个由用户帐户 ID 和 sku 组合创建的令牌。然后检查我的服务器以验证购买。有没有办法获取用户的帐户ID?我希望通过一次购买就可以在多个设备上使用该应用程序。如果用户没有使用任何社交 api 登录,是否有办法跨多个设备验证用户?
经过反复试验和研究,我找到了解决方案。所以对于可能具有相同 need/issue:
的任何其他人
首先,将此添加到您的 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。
我已经为订阅服务实施了应用内结算。一切都很好,但我正处于需要确保安全的地步。我收到的各种建议 运行 建议通过加号 API 使用登录用户的帐户 ID。但是,如果用户不使用他们的 gmail 帐户登录,我将如何获得此信息?我的想法是生成一个由用户帐户 ID 和 sku 组合创建的令牌。然后检查我的服务器以验证购买。有没有办法获取用户的帐户ID?我希望通过一次购买就可以在多个设备上使用该应用程序。如果用户没有使用任何社交 api 登录,是否有办法跨多个设备验证用户?
经过反复试验和研究,我找到了解决方案。所以对于可能具有相同 need/issue:
的任何其他人首先,将此添加到您的 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。