Gmail API会返回403错误代码和&#34; <user email =“”>&#34; </user>拒绝委托

时间:2014-10-01 06:25:12

标签: oauth-2.0 google-oauth gmail-api google-api-php-client

检索包含此错误的邮件时,某个域的Gmail API失败:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 OK
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Delegation denied for <user email>",
    "reason" : "forbidden"
  } ],
  "message" : "Delegation denied for <user email>"
}

我正在使用OAuth 2.0和Google Apps域范围的授权来访问用户数据。域已授予应用程序的数据访问权限。

5 个答案:

答案 0 :(得分:41)

似乎最好的办法就是在请求中始终使用userId =“me”。这告诉API只使用经过身份验证的用户的邮箱 - 无需依赖电子邮件地址。

答案 1 :(得分:1)

我们的用户已迁移到域中,并且他们的帐户附加了别名。我们需要将SendAs地址默认为其中一个导入的别名,并希望有一种方法来自动化它。 Gmail API看起来像是解决方案,但是我们的特权用户有权对帐户进行更改 - 但我们一直看到&#34;代表团拒绝了&#34; 403错误。

以下是我们如何列出其SendAs设置的PHP示例。

<?PHP

//
// Description:
//   List the user's SendAs addresses.
//
// Documentation:
//   https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs
//   https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/list
//
// Local Path:
//   /path/to/api/vendor/google/apiclient-services/src/Google/Service/Gmail.php
//   /path/to/api/vendor/google/apiclient-services/src/Google/Service/Gmail/Resource/UsersSettingsSendAs.php
//
// Version:
//    Google_Client::LIBVER  == 2.1.1
//

require_once $API_PATH . '/path/to/google-api-php-client/vendor/autoload.php';

date_default_timezone_set('America/Los_Angeles');

// this is the service account json file used to make api calls within our domain
$serviceAccount = '/path/to/service-account-with-domain-wide-delagation.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $serviceAccount );

$userKey = 'someuser@my.domain';

// In the Admin Directory API, we may do things like create accounts with 
// an account having roles to make changes. With the Gmail API, we cannot 
// use those accounts to make changes. Instead, we impersonate
// the user to manage their account.

$impersonateUser = $userKey;

// these are the scope(s) used.
define('SCOPES', implode(' ', array( Google_Service_Gmail::GMAIL_SETTINGS_BASIC ) ) );

$client = new Google_Client();
$client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
$client->setScopes(SCOPES); // adds the scopes
$client->setSubject($impersonateUser);  // account authorized to perform operation

$gmailObj  = new Google_Service_Gmail($client);

$res       = $gmailObj->users_settings_sendAs->listUsersSettingsSendAs($userKey);

print_r($res);


?>

答案 2 :(得分:1)

我之前遇到过同样的问题,解决方案非常棘手,您需要首先模拟需要访问gmail内容的人员,然后使用userId ='me'来运行查询。它对我有用。

下面是一些示例代码:

this

答案 3 :(得分:0)

我想访问新的电子邮件ID /帐户的电子邮件,但是发生了什么事,最近创建的包含JSON的带有'.credentials'的文件夹与我先前尝试过的先前的电子邮件ID /帐户相关联。 JSON中存在的访问令牌和其他参数未与新的电子邮件ID /帐户相关联。因此,为了使其运行,您只需要删除'.credentails'文件夹并再次运行该程序即可。现在,该程序将打开浏览器,并要求您授予权限。

要删除包含python文件的文件夹

import shutil
shutil.rmtree("path of the folder to be deleted")

您可以在程序末尾添加

答案 4 :(得分:0)

最近,我开始探索Gmail API,并且遵循与郭提到的相同的方法。但是,当我们的用户数量或更多时,这将需要时间和太多的呼叫。在进行域范围的委派后,我期望管理员ID能够访问委派的收件箱,但似乎我们需要为每个用户创建服务。