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 Domain-Wide授权来访问用户数据。域已向应用程序授予数据访问权限。
发布于 2014-10-02 08:50:05
似乎最好的做法是在你的请求中总是有userId="me“。这告诉API只使用经过身份验证的用户的邮箱--不需要依赖电子邮件地址。
发布于 2017-01-27 09:12:41
我们的用户已经迁移到一个域中,他们的帐户有别名。我们需要将SendAs地址默认为其中一个导入的别名,并希望有一种方法使其自动化。Gmail API看起来像是解决方案,但是我们的特权用户拥有更改帐户的角色并不起作用--我们一直看到"Delegation denied for“403错误。
下面是一个PHP示例,说明我们如何列出他们的SendAs设置。
<?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);
?>发布于 2020-06-02 09:07:23
我之前也遇到过同样的问题,解决方案非常棘手,你需要首先模拟你需要访问gmail内容的人,然后使用userId='me‘来运行查询。这对我很管用。
下面是一些示例代码:
users = # coming from directory service
for user in users:
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
####IMPORTANT######
credentials_delegated = credentials.with_subject(user['primaryEmail'])
gmail_service = build('gmail', 'v1', credentials=credentials_delegated)
results = gmail_service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
for label in labels:
print(label['name'])https://stackoverflow.com/questions/26135310
复制相似问题