G + using Google Play Services to access the user's email. Missing android.permission.GET_ACCOUNTS

I am working on an application that has a social login feature. Therefore, I created an android library project, the role of which is provided by a social login using g +, FB, etc. My application uses this android library project for social login. But now I am faced with the problem described below.

I successfully logged in using g +.

But when I try to get a subscription in a user’s email

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

I get 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) 

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 someone explain to me what happened?

+5
source share
3 answers

I think your manifest is garbled, try the following:

 <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> 

See the Android documentation for the manifest for more information.

+5
source

If this is a problem for Android version 6 (or higher) and you are compiling it with the Android SDK 23 and you have already double checked that the GET_ACCOUNTS permission is in the manifest, then the problem may be the new rules for resolving Runtime for Android.

From version 6 of Android, users need permission to receive permissions when the application first uses these permissions (even if it is already in the manifest).

You can solve this problem as follows:

Put this piece 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

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

When the user allows or denies permission, the onRequestPermissionsResult method is called, after which you can request an 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 the new rules for permissions, I recommend that you read this article http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

+10
source

The manifest entries are wrong, you need to place permissions and use permissions outside the application tag

0
source

Source: https://habr.com/ru/post/1216672/


All Articles