gpt4 book ai didi

java - 获取身份验证 token 在帐户管理器中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:50 25 4
gpt4 key购买 nike

我已经在我的 Android 应用程序中实现了帐户管理器。我可以毫无问题地添加帐户并获取身份验证 token ,并且签名的 apk 也可以正常工作。在我为 build.grade 添加新风格之前,流程一直运行良好:

productFlavors {
release {
applicationId "com.faranegar.flight.release"
versionName "1.0.0"
buildConfigField 'String', 'BASE_API', '"http://release.mysite.ir/api/"'
buildConfigField 'String', 'BASE_MOBILE_API', '"https://releaseapp.mysite.ir/api/"'

}
demo {
applicationId "com.faranegar.flight.demo"
buildConfigField 'String', 'BASE_API', '"http://demo.mysite.ir/api/"'
buildConfigField 'String', 'BASE_MOBILE_API', '"http://demoapp.mysite.ir/api/"'
versionName "1.0.0"

}
}

但是在签名 apk 中,当在 gradle 中添加 flavor 时,获取身份验证 token 不起作用。 我得到的身份验证 token 如下:

final AccountManager accountManager = AccountManager.get(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.GET_ACCOUNTS},
Constants.GET_ACCOUNT_PERMISSION);
return;
}
final Account[] accounts = accountManager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
for (final Account account : accounts) {
if (account.name.equals(Utils.getUserName(context))) {

final AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AccountGeneral.ACCOUNT_TYPE, null, (Activity) context, null, null);
new Thread(new Runnable() {
@Override
public void run() {
try {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "bundle token", Toast.LENGTH_SHORT).show();
}
});
Bundle bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
accountControllerListener.onGetToken(authtoken);
} catch (final Exception e) {
sendErrorToRetrofit(e);
e.printStackTrace();
}
}
}).start();
}
}

当调用“Bundle bnd = future.getResult();”时,我收到此错误

android.accounts.AccountManager.convertErrorToException(AccountManager.java:2153)
android.accounts.AccountManager.access$500(AccountManager.java:149)

android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1996) android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)

android.os.Binder.execTransact(Binder.java:453)

注意:当我跟踪时,我发现 MyAuthenticator 中的 getAuthToken 方法从未在签名的 apk 中调用。

最佳答案

我认为你以错误的方式抗拒了味道。查看下面的示例代码,了解定义风格与定义构建类型有何不同:

不同的构建类型:

You can create and configure build types in the module-level build.gradle file inside the android {} block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true. This allows you to debug the app on secure Android devices and configures APK signing with a generic debug keystore.

    buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
applicationIdSuffix ".debug"
}

}

不同口味:

    productFlavors {
demo {
applicationIdSuffix ".demo"
versionNameSuffix "-demo"
}
full {
applicationIdSuffix ".full"
versionNameSuffix "-full"
}
}

After you create and configure your product flavors, click Sync Now in the notification bar. After the sync completes, Gradle automatically creates build variants based on your build types and product flavors, and names them according to . For example, if you created 'demo' and 'full' product flavors, and kept the default 'debug' and 'release' build types, Gradle creates the following build variants:

  • 演示调试

  • 演示版本

  • 完全调试

  • 完整发布

执行此操作并检查问题是否仍然存在。

关于java - 获取身份验证 token 在帐户管理器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com