Gmail API: Insufficient Resolution

SITUATION:

I am testing the Gmail API for my application.

I tested several queries and they work fine. For example, get messages, get user history, get a draft, etc.

Basically, all read-only requests work fine.

I have some issues related to resolving with other requests, for example, when I have to write or delete a draft.

This is the error I get:

(403) Insufficient Permission 

CODE:

This is the application initialization function:

 public function gmail_init_service() { $client = new Google_Client(); $client->setApplicationName("Gmail API test"); $client->setDeveloperKey("MY_KEY"); $client->setClientSecret('MY_CLIENT_SECRET'); $client->SetClientId('MY_CLIENT_ID'); $client->setScopes(array('https://mail.google.com/')); $client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN","token_type":"Bearer","expires_in":3600,"refresh_token":"MY_REFRESH_TOKEN","created":1433502343}'); $service = new Google_Service_Gmail($client); return $service; } 

This is a request to delete one draft:

 public function gmail_delete_draft() { $service = $this->gmail_init_service(); // --------------- Get draft list -------------- $list = $service->users_drafts->listUsersDrafts('me'); $draftList = $list->getDrafts(); // --------------- Get draft IDs --------------- $inbox_draft = []; foreach($draftList as $mlist) { $draftId = $mlist->id; $optParamsGet2['format'] = 'full'; $single_message = $service->users_drafts->get('me', $draftId , $optParamsGet2); $inbox_draft[]['draftId'] = $draftId; $inbox_draft[]['draft'] = $single_message; } // --------------- Delete draft --------------- $draft_delete = $service->users_drafts->delete('me', 'DRAFT_ID' ); } 

EDIT:

I tried to revoke permission and set new credentials. The scope declared at service initialization:

 https://mail.google.com/ 

as stated in the documentation, provide full access to the account.

But I still get the same error. Exact error for the following queries:

Delete draft - Create draft - Create label - Delete message

QUESTION:

Why am I getting this error?
Is this due to the same storage of values ​​in the cache?
Or is it related to API permission?

+5
source share
2 answers

You need https://www.googleapis.com/auth/gmail.compose 'to create a draft. So what happens if you

 $client->setScopes(array( 'https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.compose' )); 

or if you want to get more formal

 define('SCOPES', implode(' ', array( Google_Service_Gmail::MAIL_GOOGLE_COM, Google_Service_Gmail::GMAIL_COMPOSE) )); $client->setScopes(SCOPES) 

or something that might be valid php (I haven't been doing php for a while).

Please note that if you already have a token, you may need to make a few words to cancel it so that you can reissue with added permissions. This may mean deleting the file, possibly with the name gmail.storage, or if you have access to the login account and go to https://security.google.com/settings/security/permissions access permissions can be manually revoked.

This link may be relevant: https://developers.google.com/gmail/api/auth/scopes

And meander through source code can be enlightened.

You may be able to collect some insights from my battle with the same things in Python

+7
source

To delete mail you need https://mail.google.com/ to delete mail.

+2
source

All Articles