简体   繁体   English

Gmail API:权限不足

[英]Gmail API: Insufficient Permission

THE SITUATION: 情况:

I am testing the Gmail API for my app. 我正在为我的应用测试Gmail API

I have tested some requests and they are working fine. 我已经测试了一些请求,它们工作正常。 For example get messages, get user history, get draft list etc.. 例如获取消息,获取用户历史记录,获取草稿列表等。

Basically all the read only requests are working fine. 基本上所有只读请求都正常工作。

I have instead some issues related with permission with other requests, for example when i have to write or delete a draft. 我有一些与其他请求的权限相关的问题,例如当我必须写或删除草稿时。

This is the error i get: 这是我得到的错误:

(403) Insufficient Permission

THE CODE: 编码:

This is the function to initialize the app: 这是初始化应用程序的功能:

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 the 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 have tried to revoke the permission and setup new credentials. 我试图撤销权限并设置新凭据。 The scope declared when initializing the service is: 初始化服务时声明的范围是:

https://mail.google.com/

that as stated in the documentation grant full access to the account . 如文档中所述,授予对帐户的完全访问权限

But i am still getting the same error. 但我仍然得到同样的错误。 The same exact error for the following requests: 以下请求的确切错误:

Delete draft - Create draft - Create label - Delete message 删除草稿 - 创建草稿 - 创建标签 - 删除消息

THE QUESTION: 问题:

Why am i getting that error? 为什么我会收到这个错误?
It has to do with same values store in a cache? 它与缓存中的相同值存储有关吗?
Or is related with permission of the API? 或者与API的许可相关?

You need ' https://www.googleapis.com/auth/gmail.compose ' to create a draft. 您需要“ https://www.googleapis.com/auth/gmail.compose ”来创建草稿。 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 whatever the valid php might be (I haven't done php for a while). 或者有效的php可能是什么(我有一段时间没有做过php)。

Note that if you have a token already you might have to do some calisthenics to revoke it so you can reissue with the added permissions. 请注意 ,如果您已经拥有令牌,则可能需要执行一些健美操以撤消它,以便您可以使用添加的权限重新发布。 That might mean deleting a file, perhaps named gmail.storage or, if you have access to the account login and make your way to https://security.google.com/settings/security/permissions the access permissions can be manually revoked. 这可能意味着删除文件,可能名为gmail.storage,或者,如果您有权访问帐户登录并转到https://security.google.com/settings/security/permissions ,则可以手动撤消访问权限。

This link might be relevant: https://developers.google.com/gmail/api/auth/scopes 此链接可能相关: https//developers.google.com/gmail/api/auth/scopes

And a meander through the source code might be enlightening. 通过源代码蜿蜒曲折可能具有启发性。

You might be able to glean some insight from my battle with this same sort of thing under Python 你可能能够在Python下用同样的东西从我的战斗中收集一些见解

您需要https://mail.google.com/作为范围才能删除邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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