尽管调用setSelectedAccountName(Android 6.0),GoogleAccountCredential名称仍为null

时间:2015-10-12 16:13:56

标签: android google-app-engine google-cloud-endpoints credentials google-account

我使用安全的后端创建了一个端点,并且自从3月份开始在我正在构建的应用程序中使用它(源文档here)。我最近在Android 6.0设备上安装了最新版本,弹出了一个奇怪的错误(它在4.2.2和5.1上完美运行)。

具体错误是:

IllegalArgumentException: the name must not be empty: null 

我跟踪了凭证的错误,您可以看到下面的代码。在Android 6.0帐户上可能是“user@gmail.com”但字符串'test'结果为null!

是否有特定的6.0更改了GoogleAccountCredential?

public static GoogleAccountCredential getCredential(Context ctx) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String account = prefs.getString(UserProfileHelper.PREF_USER_ACCOUNT, "");
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(ctx,
            "server:client_id:MY_ACCOUNT_NUMS.apps.googleusercontent.com")
            .setSelectedAccountName(account);


    String test = credential.getSelectedAccountName();
    return credential;
}

1 个答案:

答案 0 :(得分:14)

是的,使用Android 6.0 Marshmallow,您现在需要在运行时https://developer.android.com/training/permissions/index.html

请求权限

要获取这些凭据,您需要CONTACTS组中的GET_ACCOUNTS权限

https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

您必须在活动/片段中请求它并处理与您的应用相关的任何UX。