简体   繁体   中英

G+ login using Google Play Services accessing user email throws Missing android.permission.GET_ACCOUNTS

I am working on an app having feature of social login. So I created a android-library project whose role is provide social login with g+,FB etc.My app is using this android-library project for social login. But now I am facing issue describe below.

I am successfully logged in using g+.

But When I am trying to get signed in user email

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

I am getting below exception stack trace

W/System.err﹕ java.lang.SecurityException: Missing android.permission.GET_ACCOUNTS
W/System.err﹕ at android.os.Parcel.readException(Parcel.java:1474)
W/System.err﹕ at android.os.Parcel.readException(Parcel.java:1427)
W/System.err﹕ at com.google.android.gms.plus.internal.zzd$zza$zza.getAccountName(Unknown Source)

I have added uses-permission tag in my android-library project. Below is the android-libray manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="login.social">

    <application >
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    </application>

</manifest>

Can anyone guide me what's wrong?

If this is a problem for Android version 6 (or higher) and you compile with Android SDK 23 and you already have double checked that GET_ACCOUNTS permission is in the Manifest then the problem might be new rules for Android's Runtime Permission.

From Android version 6 users must be asked for some permissions when the application is using these permissions for the first time (even if it is already in Manifest).

You can solve this problem like this:

Put this part of code

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

    int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.GET_ACCOUNTS);
    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] {Manifest.permission.GET_ACCOUNTS}, 
            REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
}

instead of

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

When user allow or disallow permission, method onRequestPermissionsResult will be called and then you can ask for email.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                Utils.showToast("Hello " + email, mContext);                      
            } else {
                // Permission Denied
                Utils.showToast("GET_ACCOUNTS Denied", mContext);
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

If you want to know more about new rules for permissions I recommend you to read this article http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

I think your manifest is malformed, try this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="login.social">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <application >
      <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    </application>
</manifest>

Have a look to the android documentation about manifest for more info.

清单条目是错误的,您需要在应用程序标记之外放置权限和使用权限

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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