Gmail API 403 Insufficient Permissions domain global
Asked Answered
G

4

6
            @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 have marked the line which is causing a 402 Insufficient permissions domain: global error. If I comment out said line the program will return the labels and print them to the screen without the permissions error. I have signed my release apk and set up the Google Play Developer console. The app is signing just fine it's SHA1 and I followed the sample application which retrieves credentials.

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

What to do about insufficient permissions?

Thank you.

The creation of 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 )
Gentilism answered 5/11, 2015 at 0:7 Comment(4)
Could you show the code where you instantiate your mService? It is probably there your permission issue is.Bandler
I'ts very boiler plate from the sample. I added that snippet. Thank you for asking.Gentilism
I have never used the Java client library, so I'm afraid I don't know. Until someone else answers, you could try your request with regular http-requests over at the Oauth Playground, and try to figure out what Gmail API permission scope you need.Bandler
I believe I have made progress on this given the information returned in the comments herewith. There is explained in the documentation of Gmail API developers.google.com/gmail/api/auth/scopes. At the top of the quick start application one will find declared a list of strings for scope and if one expands the scope now the code will run without the permissions error.Gentilism
G
8
    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 scopes instead of the default with only GMAIL_LABELS worked for me.

Gentilism answered 6/11, 2015 at 1:50 Comment(0)
O
3

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

Observatory answered 19/11, 2015 at 14:44 Comment(0)
M
0

You may also need to delete your previous credentials file after changing permissions. It's token.json in the current folder.

Medical answered 23/12, 2018 at 18:26 Comment(0)
W
0

Apart from setting scopes mentioned by @Eae, delete StoredCredential file, path of file is your_project/tokens/StoredCredential.

Winebibber answered 14/8, 2020 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.