Gmail API 403 Insufficient permissions for global domain

@Override protected List< String > doInBackground( Void... params ) { try { //This line below is the cause of the insufficient permissions error ListMessagesResponse messagesWithLabels = mService.users().messages().list("me").setQ("label:inbox").execute(); /*for( Message m : messagesWithLabels.getMessages( ) ) { if( m.size( ) > MAXSIZE ) { List<MessagePartHeader> headers = m.getPayload( ).getHeaders( ); for ( MessagePartHeader header : headers ) { if ( header.getName( ).equals("From") || header.getName().equals("Date") || header.getName().equals("Subject") || header.getName().equals("To") || header.getName().equals("CC")) { messageDetails.put( header.getName( ).toLowerCase( ), header.getValue( ) ); } } messageDetails.put("snippet", m.getSnippet()); messageDetails.put("threadId", m.getThreadId()); messageDetails.put("id", m.getId()); messageDetails.put("body",m.getPayload().getBody().getData()); GmailFunctions.deleteThread( mService, "me", m.getId( ) ); messageDetails.clear( ); } }*/ return getDataFromApi( ); } catch ( Exception e ) { mLastError = e; cancel( true ); return null; } } 

I highlighted a line that causes 402 Invalid rights domain: global error. If I comment on the specified line, the program will return the labels and print them on the screen without a permission error. I signed my apk version and set up the Google Play Developer Console. The application only signs the SHA1 penalty, and I followed an example of an application that retrieves credentials.

https://developers.google.com/gmail/api/quickstart/java

What to do with insufficient permissions?

Thanks.

Creating mservice:

  private class MakeRequestTask extends AsyncTask< Void, Void, List< String > > { private com.google.api.services.gmail.Gmail mService = null; private Exception mLastError = null; ArrayList<String> sRemovalIds = new ArrayList<String>( ); List< String > inserts = new ArrayList< String >( ); Map<String, Object> messageDetails = new HashMap<String, Object>( ); public MakeRequestTask( GoogleAccountCredential credential ) { HttpTransport transport = AndroidHttp.newCompatibleTransport( ); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance( ); mService = new com.google.api.services.gmail.Gmail.Builder( transport, jsonFactory, credential ) .setApplicationName( "Gmail API Android Quickstart" ) .build( ); } @Override protected List< String > doInBackground( Void... params ) { try { ListMessagesResponse messagesWithLabels = mService.users().messages().list("me").setQ("label:inbox").execute(); /*for( Message m : messagesWithLabels.getMessages( ) ) { if( m.size( ) > MAXSIZE ) 
+5
source share
2 answers
  private static final String[ ] SCOPES = { GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM }; 

Using these areas instead of the default, only GMAIL_LABELS worked for me.

+6
source

You may also need to delete the previous credential file after changing permissions. This is usually in $ HOME / .credentials /

+2
source

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


All Articles