我正在尝试获取 Google Drive API 的访问令牌,以便将其用于来自我的 Python 脚本的后续请求。但是我收到了一个无效的 grant_type 错误,这有点令人困惑。
我在用oauthlib
and requests_oauthlib
since oauth2client
已正式弃用。
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url='https://accounts.google.com/o/oauth2/token', client_id=client_id, client_secret=client_secret, scope="https://www.googleapis.com/auth/drive.readonly")
这是我得到的错误
回溯(最近一次调用):文件“sample.py”,第 10 行,位于
令牌 = oauth.fetch_token(token_url='https://accounts.google.com/o/oauth2/token https://accounts.google.com/o/oauth2/token',
client_id=client_id, client_secret=client_secret,
范围 =“https://www.googleapis.com/auth/drive.readonly”)文件
“/Users/kaybus/anaconda/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py”,
第 244 行,在 fetch_token 中
self._client.parse_request_body_response(r.text,scope=self.scope) 文件
“/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/base.py”,
第 408 行,在 parse_request_body_response 中
self.token = parse_token_response(body,scope=scope)文件“/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py”,
第 379 行,在 parse_token_response 中
validate_token_parameters(params) 文件“/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py”,
第 386 行,在 validate_token_parameters 中
raise_from_error(params.get('error'), params) 文件“/Users/kaybus/anaconda/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/errors.py”,
raise_from_error 中的第 406 行
引发 cls(**kwargs) oauthlib.oauth2.rfc6749.errors.UnsupportedGrantTypeError:
(unsupported_grant_type) 无效的 grant_type:client_credentials
相信很多人可能都遇到过这个问题。我是否在做一些根本性错误的事情?请告诉我你是如何解决的。提前致谢。