【发布时间】:2019-08-16 02:44:34
【问题描述】:
我正在尝试通过他们的 API 修改 GMail 收件箱。
但我得到了错误:
请求时出现 HttpError 403 https://www.googleapis.com/gmail/v1/users/me/labels/SOME_MESSAGE_ID 返回“权限不足”
我已使用相同的 OAuth 凭据下载我的消息。所以我知道这是有效的。
我检查标签范围是否可用。
我看不出我做错了什么。 The label doc doesn't help.
有什么可以解释的吗?
def archive(msg_id):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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(
'credentials.json', ArchiveScope)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
msg_labels = {'removeLabelIds': ['INBOX'], 'addLabelIds': ['MyLabel']}
service.users().labels().update(userId='me', id=msg_id, body=msg_labels).execute()
print('Message ID: %s' % msg_id)
【问题讨论】:
-
您提到您正在尝试修改 Gmail 标签,您是否添加了以下范围? (
https://mail.google.com/、https://www.googleapis.com/auth/gmail.modify、https://www.googleapis.com/auth/gmail.labels)。另外,this SO post 使用 C# 语言,如果建议的操作适用于您,您可以尝试一下吗?
标签: python python-3.x gmail gmail-api google-api-client