python - "Requested entity was not found."当管理员尝试修补另一个用户签名时

标签 python google-api gmail-api google-workspace google-api-python-client

我正在尝试通过 Gmail API 通过 Python 设置用户签名。当使用我自己的管理员用户执行此操作时,它工作正常,但当使用不同的电子邮件地址尝试时,我收到此错误:

googleapiclient.errors.HttpError: <HttpError 404 when requesting https://gmail.googleapis.com/gmail/v1/users/me/settings/sendAs/[email protected]?alt=json returned "Requested entity was not found.". Details: "[{'message': 'Requested entity was not found.', 'domain': 'global', 'reason': 'notFound'}]">

这是我的 Python 代码:

def main():
SCOPES = 'https://www.googleapis.com/auth/gmail.settings.basic'
# Check if token.pickle exists
if os.path.exists(client_token_pickle):
    creds = Credentials.from_authorized_user_file(client_token_pickle, SCOPES)

# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(client_oauth_file, scopes)
        creds = flow.run_local_server(port=8080)
    # Save the credentials for the next run
    with open(client_token_pickle, "w") as token:
        token.write(creds.to_json())

service = build('gmail', 'v1', credentials=creds)

signature = {'signature': 'lalala'}
service.users().settings().sendAs().patch(userId='me',sendAsEmail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a7b6a0a78cb6beb2babf93a0beb2a5b2fdb7b6" rel="noreferrer noopener nofollow">[email protected]</a>", body=signature).execute()

使用 GAM 可以正常工作,所以应该可以,但是如何实现呢?非常感谢您的任何意见!

最佳答案

Requested entity was not found.

表示当前经过身份验证的用户不存在您尝试执行操作的实体。

When doing it with my own admin user it works fine but when trying it with a different email address I'm getting this error

当您授权该代码时,您将其授权为特定用户。您只能对该用户进行更改。

即使用户是工作区域的管理员,他们也无法更改其他人的数据。您需要以该用户的身份授权代码,或者使用服务帐户并委托(delegate)给该用户。

对用户的委托(delegate)

要使服务帐户正常工作,需要授予 domain wide delegation这将使其能够代表域中的用户执行操作。

在直接来自上面链接的文档的示例中

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

# Email of the Service Account
SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com'

# Path to the Service Account's Private Key file
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/<public_key_fingerprint>-privatekey.p12'

def create_directory_service(user_email):
    """Build and returns an Admin SDK Directory service object authorized with the service accounts
    that act on behalf of the given user.

    Args:
      user_email: The email of the user. Needs permissions to access the Admin APIs.
    Returns:
      Admin SDK directory service object.
    """

    credentials = ServiceAccountCredentials.from_p12_keyfile(
        SERVICE_ACCOUNT_EMAIL,
        SERVICE_ACCOUNT_PKCS12_FILE_PATH,
        'notasecret',
        scopes=['https://www.googleapis.com/auth/admin.directory.user'])

    credentials = credentials.create_delegated(user_email)

    return build('admin', 'directory_v1', credentials=credentials)

你可以看到他们如何使用

credentials = credentials.create_delegated(user_email)

告诉服务帐户要将其委托(delegate)给哪个用户。

请注意,标准示例用于管理目录 api。您需要更改它才能与 gmail api 一起使用。但不需要改变太多。

关于python - "Requested entity was not found."当管理员尝试修补另一个用户签名时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72997848/

相关文章:

python素数生成

python - 如何为非常相似的代码片段编写方法或 for 循环

java - 电子邮件 API ?如何使用原始格式的 Gmail api 请求电子邮件以及如何将其转换为普通字符串

javascript - 使用 JavaScript Youtube API 上传文件 - 未定义 MediaUploader

javascript - 是否必须使用 google apps 帐户才能使用 Google+ Domain API?

c# - 如何使用 Gmail API 阅读整封邮件

java - Gmail API : Decoding the body of the message (Java/Android)

python - .isin() 比 .query() 快吗

python - 谷歌应用引擎 : ImportError: No module named appengine. 分机

c# - 如何在 C# 中读取 (Google.Apis.Drive.v3.Data.File)?