为什么 Gmail API returns 401 错误?
Why does the Gmail API returns 401 error?
我向 gmail 发送批量请求后的响应与文档中描述的相同 (https://developers.google.com/gmail/api/guides/handle-errors#exponential-backoff):
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
我的请求码是:
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
elif 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', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run.
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
gmailUrl = "https://gmail.googleapis.com/batch/gmail/v1"
request_header = {"Authorization": f"Bearer {creds}", "Host": "www.googleapis.com", "Content-Type": "multipart/mixed; boundary=boundary"}
body = []
for n in message_Ids:
boundary = "--boundary\n"
content_type = "Content-Type: application/http\n\n"
request = f'GET /gmail/v1/users/me/messages/{n}\n'
requestObj = boundary + content_type + request + "Accept: application/json; charset=UTF-8\n"
body.append(requestObj)
body.append("--boundary")
body = "\n".join(body)
response = req.post(url=gmailUrl, headers=request_header, data=body, auth=)
pprint(response)
pprint(response.text)
当我设法以某种方式从 gmail 服务器获得响应时,我想我的请求已被接受。
但是我不明白为什么会出现 401 错误。
如果我将 GET 请求作为单个请求发送,我的应用程序工作正常。
我必须在 "Autorization": f"Bearer {creds}"
行中输入什么?
提前致谢!
正如我在问题下方的评论中所指出的,我的错误是我确实提供了凭证实例,而不是令牌本身。
如 comments 中所述,您在 Authorization
header:
中提供了 credentials
的实例
"Authorization": f"Bearer {creds}"
您应该提供凭据“access_token
:
"Authorization": f"Bearer {creds.token}"
参考:
我向 gmail 发送批量请求后的响应与文档中描述的相同 (https://developers.google.com/gmail/api/guides/handle-errors#exponential-backoff):
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
我的请求码是:
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
elif 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', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run.
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
gmailUrl = "https://gmail.googleapis.com/batch/gmail/v1"
request_header = {"Authorization": f"Bearer {creds}", "Host": "www.googleapis.com", "Content-Type": "multipart/mixed; boundary=boundary"}
body = []
for n in message_Ids:
boundary = "--boundary\n"
content_type = "Content-Type: application/http\n\n"
request = f'GET /gmail/v1/users/me/messages/{n}\n'
requestObj = boundary + content_type + request + "Accept: application/json; charset=UTF-8\n"
body.append(requestObj)
body.append("--boundary")
body = "\n".join(body)
response = req.post(url=gmailUrl, headers=request_header, data=body, auth=)
pprint(response)
pprint(response.text)
当我设法以某种方式从 gmail 服务器获得响应时,我想我的请求已被接受。 但是我不明白为什么会出现 401 错误。 如果我将 GET 请求作为单个请求发送,我的应用程序工作正常。
我必须在 "Autorization": f"Bearer {creds}"
行中输入什么?
提前致谢!
正如我在问题下方的评论中所指出的,我的错误是我确实提供了凭证实例,而不是令牌本身。
如 comments 中所述,您在 Authorization
header:
credentials
的实例
"Authorization": f"Bearer {creds}"
您应该提供凭据“access_token
:
"Authorization": f"Bearer {creds.token}"