9

I've created an endpoint using a secured backend and have been using it since March on an app I'm building (source docs here). I recently installed the latest version to my Android 6.0 device and an odd error popped up (it works perfectly on 4.2.2 & 5.1).

The specific error is:

IllegalArgumentException: the name must not be empty: null 

Which I traced to an error with the credential, you can see the code below. On Android 6.0 account may be "user@gmail.com" but the string 'test' turns out to be null!

Is there something specific about 6.0 that changed 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;
}
easycheese
  • 5,859
  • 10
  • 53
  • 87

1 Answers1

14

Yes with Android 6.0 Marshmallow, you will now need to request permissions at run time https://developer.android.com/training/permissions/index.html

In order to get those credentials you need the GET_ACCOUNTS permission in the CONTACTS group

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

You will have to request it in your activity/fragment and handle any UX pertaining to your app.

AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
  • Oh crap ... Didn't even think about that, you would think lint would catch it.... Or that a better error would be thrown. You are probably right, I'll test it though. – easycheese Oct 12 '15 at 16:26
  • @easycheese adding in manifest will only work on devices below 6.0, for 6.0 you have to add some code for requesting the permissions – AndroidEnthusiast Oct 12 '15 at 16:28
  • Yeah, manually approved the permission and the sync worked. Can't believe I missed that... – easycheese Oct 12 '15 at 16:28
  • @easycheese you can manually approve it for the moment but don't forget to add the code for requesting it. – AndroidEnthusiast Oct 12 '15 at 16:31
  • 2
    Yeah, I'm walking the dog so that was the easiest way... Thanks – easycheese Oct 12 '15 at 16:31
  • @AndroidEnthusiast , I am stuck with same problem you mentioned in your post, I went through this link https://developers.google.com/android/guides/http-auth#retrieve_the_account_name but my credentials.getSelectedAccountName returns null in Android 6.0 , Can you show me light. I am new to Android. – shank2111 Jan 22 '16 at 07:47
  • @shank2111 have you added the required permissions and done the permission workflow? – AndroidEnthusiast Jan 22 '16 at 11:17
  • @AndroidEnthusiast i am also facing same issue. First time email selection prompt is opening then saving selected email using mCredential.setSelectedAccountName(accountName); method, again trying adding calendar event , mCredential.getSelectedAccountName() is returning null. – Ravikumar11 Mar 23 '17 at 05:45