Python 3.x 谷歌API-不全面“;无效凭证“;

Python 3.x 谷歌API-不全面“;无效凭证“;,python-3.x,google-api,google-oauth,Python 3.x,Google Api,Google Oauth,我正在尝试将Python3.5代码的两部分结合起来,以便与googlegmailapi一起工作。一个是使用刷新密钥获取访问密钥,并确认其有效。另一个是通过API获取Gmail列表,并确认其有效。当我合并这两个代码时,收到“无效凭据”错误消息 代码片段1:获取刷新密钥(此操作有效) 我试图找出问题出在哪里,但无法找出根本原因。。 你能指出我的密码有什么问题吗?为什么代码段1+2不起作用?我需要在Python请求模块中执行某些操作吗?401:无效凭据您使用的访问令牌已过期或无效。这并不能解释组合代码

我正在尝试将Python3.5代码的两部分结合起来,以便与googlegmailapi一起工作。一个是使用刷新密钥获取访问密钥,并确认其有效。另一个是通过API获取Gmail列表,并确认其有效。当我合并这两个代码时,收到“无效凭据”错误消息

代码片段1:获取刷新密钥(此操作有效) 我试图找出问题出在哪里,但无法找出根本原因。。
你能指出我的密码有什么问题吗?为什么代码段1+2不起作用?我需要在Python请求模块中执行某些操作吗?

401:无效凭据您使用的访问令牌已过期或无效。这并不能解释组合代码无法工作的原因。事实上,我每次运行gmail API时都会新生成密钥,所以它永远不会过期。我还确认snippet1返回了正确的密钥。所以我只是嵌入了gmail Api。所以,它不应该是无效的。谷歌的认证服务器说它不好。我的钱都花在谷歌上了。你有什么理由想这么做而不只是使用谷歌的Python客户端库?你可以试着把他们的代码拆开,看看哪里出了问题。
import requests
import json
import string

translator = str.maketrans({key: None for key in string.punctuation})

payload = {'grant_type': 'refresh_token',
       'refresh_token': '<--- Token ---->',
       'client_id': '<----Client ID---->',
       'client_secret': '<----Client secret---->',
       'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
       'scope': 'https://mail.google.com/'
      }
r = requests.post("https://accounts.google.com/o/oauth2/token", data=payload)
txt = r.text
key_json = json.loads(r.text)
key = json.dumps(key_json['access_token'], sort_keys = True, indent = 4)
print (key.translate(translator))
import requests

Token = "<----Access Token------>"
Gmail_list ='https://www.googleapis.com/gmail/v1/users/me/messages/'
data = {'Authorization': 'Bearer ' + Token}
r3 = requests.get(Gmail_list, headers=data)
txt = r3.text
print (txt)
import requests
import json
import string
import base64
import email

translator = str.maketrans({key: None for key in string.punctuation})
Gmail_list ='https://www.googleapis.com/gmail/v1/users/me/messages/'

payload = {'grant_type': 'refresh_token',
       'refresh_token': '<----Token---->',
       'client_id': '<-----Client ID------>',
       'client_secret': '<-------Client secret------>',
       'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
       'scope': 'https://mail.google.com/'
      }
r = requests.post("https://accounts.google.com/o/oauth2/token", data=payload)
key_json = json.loads(r.text)
key1 = (json.dumps(key_json['access_token'], sort_keys = True, indent = 4))
key2 = (key1.translate(translator))
r2 = {'Authorization': 'Bearer ' + key2}
r3 = requests.get(Gmail_list, headers=r2)
txt = r3.text
print (txt)
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}