简体   繁体   English

获取身份验证令牌在客户经理中无效

[英]Get auth token doesn't work in account manager

I have implemented account manager in my android app. 我在我的Android应用程序中实现了帐户管理器。 I can add accounts and get auth tokens without any problem, and signed-apk also works correctly. 我可以添加帐户并获得auth令牌,没有任何问题,signed-apk也可以正常工作。 The flow was working perfectly untill I added a new flavor to my build.grade : 流程工作得很好,直到我为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"

    }
}

BUT in signed-apk get auth token doesn't work when flavor is added in gradle. 但是,在gradle中添加flavor时,使用signed-apk get auth令牌不起作用。 I get auth token as this: 我得到身份验证令牌:

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

And I get this error when called 'Bundle bnd = future.getResult();' 我在调用'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)

NOTE: When I traced I found out that getAuthToken method in MyAuthenticator never called in signed-apk. 注意:当我跟踪时,我发现MyAuthenticator中的getAuthToken方法从未在signed-apk中调用过。

You have defiened the flavor in a wrong way, i think. 我想,你已经以错误的方式蔑视了这种味道。 Take a look at sample code below to see how defining a flavor is different from defining a build type: 看一下下面的示例代码,看看定义一个flavor与定义一个构建类型有什么不同:

Different build types: 不同的构建类型:

You can create and configure build types in the module-level build.gradle file inside the android {} block. 您可以在android {}块内的模块级build.gradle文件中创建和配置构建类型。 When you create a new module, Android Studio automatically creates the debug and release build types for you. 创建新模块时,Android Studio会自动为您创建调试和发布构建类型。 Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true. 虽然调试版本类型没有出现在构建配置文件中,但Android Studio使用debuggable true配置它。 This allows you to debug the app on secure Android devices and configures APK signing with a generic debug keystore. 这允许您在安全的Android设备上调试应用程序,并使用通用调试密钥库配置APK签名。

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

    debug {
        applicationIdSuffix ".debug"
    }

    }

Different flavors: 不同口味:

    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 . 同步完成后,Gradle会根据您的构建类型和产品风格自动创建构建变体,并根据它们命名。 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: 例如,如果您创建了“demo”和“full”产品风格,并保留了默认的“debug”和“release”构建类型,Gradle将创建以下构建变体:

  • demoDebug demoDebug

  • demoRelease demoRelease

  • fullDebug fullDebug

  • fullRelease fullRelease

Do this and check if the problem still exists or not. 执行此操作并检查问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM