【发布时间】:2019-04-18 07:50:15
【问题描述】:
我正在尝试请求用户 gmail 数据列表消息并修改其中之一。我收到以下错误
HTTPERROR 403 “权限不足”
代码
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
response = service.users().messages().list(userId='me',
labelIds='INBOX').execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
#print(messages)
for i in messages:
aux_id = i
id = aux_id.get('id')
print(id)
message = service.users().messages().get(userId='me', id=id).execute()
#print (type(message))
data = message.get('payload', {}).get('body',{}).get('data')
data = data.decode('base64')
list_of_dic = message.get('payload',{}).get('headers')
#print(list_of_dic)
#DATE, JUST NEED TO FORMAT IT TO YYYY/MM/DD
for i in list_of_dic:
if i['name'] == 'Date':
aux_date = i
if i['name'] == 'From':
aux_sender = i
if i['name'] == 'Subject':
aux_subject = i
sender = aux_sender.get('value')
date = aux_date.get('value')
subject = aux_subject.get('value')
print(date)
print(sender)
print(subject)
print(data)## data from emaail working
print('***************************************************')
message = service.users().messages().modify(userId='me', id=id,
body='INBOX').execute()
【问题讨论】:
-
能否添加此错误的跟踪日志?
-
欢迎来到 Stack Overflow。要问一个好问题,不仅仅是复制/粘贴您的代码和错误消息。请阅读How do I ask a good question 并改进您的问题。
-
@KlausD。我已经编辑了他的问题,我希望这些信息足以让您清楚地了解这个问题。通过阅读代码,我能够理解问题,但我添加了文本以使其更清晰。
标签: python google-api gmail-api google-api-python-client