Gmail API:权限不足 [英] Gmail API: Insufficient Permission

查看:725
本文介绍了Gmail API:权限不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

状况



我正在测试 Gmail API



我已经测试了一些请求,他们正常工作。
例如获取消息,获取用户历史记录,获取草稿列表等。



基本上所有只读请求正常工作



我已经提出了与其他请求相关的一些与许可相关的问题,例如我必须写或删除一个草稿。



这是我得到的错误:

 (403)权限不足

代码



这是初始化应用程序:

  public function gmail_init_service()
{
$ client = new Google_Client();

$ client-> setApplicationName(Gmail API测试);
$ 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:承载,expires_in:3600,refresh_token:MY_REFRESH_TOKEN,created:1433502343} );

$ service = new Google_Service_Gmail($ client);

return $ service;
}

这是删除一个草案的请求:

  public function gmail_delete_draft()
{
$ service = $ this-> gmail_init_service();

// ---------------获取草稿清单--------------

$ list = $ service-> users_drafts-> listUsersDrafts('me');
$ draftList = $ list-> getDrafts();

// ---------------获取草稿编号---------------

$ inbox_draft = [];

foreach($ draftList为$ 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;
}

// ---------------删除草稿---------------

$ draft_delete = $ service-> users_drafts-> delete('me','DRAFT_ID');
}

编辑:



我已经尝试撤销权限并设置新的凭据。
初始化服务时声明的范围是:

  https://mail.google.com/ 
/ b


如文档中所述,授予帐户的完整访问权限



但我仍然收到相同的错误。
以下请求的确切错误:



删除草稿 - 创建草稿 - 创建标签 - 删除邮件



问题:



为什么我得到这个错误?

它与相同的值存储在缓存中?

或与API的许可相关?

解决方案

您需要' a href =https://www.googleapis.com/auth/gmail.compose =nofollow noreferrer> https://www.googleapis.com/auth/gmail.compose '来创建一个草案。那么如果你

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

或如果您想获得更正式的

  define('SCOPES',implode('',array(
Google_Service_Gmail :: MAIL_GOOGLE_COM,
Google_Service_Gmail :: GMAIL_COMPOSE)
));

$ client-> setScopes(SCOPES)

或任何有效的php可能(我没有做过php一段时间)。



注意,如果你已经有一个令牌,你可能需要做一些戒律可以撤销它,以便您可以使用添加的权限重新发行。这可能意味着删除文件,也许命名为 gmail.storage ,或者,如果您有权访问帐户登录名,并进入 https://security.google.com/settings/security/permissions ,可以手动撤销访问权限。



此链接可能是相关的:
https://开发人员。 google.com/gmail/api/auth/scopes



迂回通过源代码可能会启发。



您可能可以收集一些洞察从我与Python同样的事情的战斗


THE SITUATION:

I am testing the Gmail API for my app.

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?

解决方案

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 whatever the valid php might be (I haven't done php for a while).

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.

This link might be relevant: 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

这篇关于Gmail API:权限不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆