“OAuth2Token”对象没有属性“authorize”,即使安装了googleauthhttplib2

2024-04-26 06:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

试图建立一个谷歌API资源,我得到一个错误说

'OAuth2Token' object has no attribute 'authorize'

我在这里读到一个解决方案是安装google-auth-httplib2,但是我已经安装了,并且仍然遇到了错误。你知道吗


class Oauth2CallbackView(View):

    def get(self, request, *args, **kwargs):
        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            CLIENT_SECRETS_FILE, scopes=SCOPES)
        flow.redirect_uri = 'http://127.0.0.1:8000/profiles/oauth2callback/'

        credentials = flow.fetch_token(code=self.request.GET.get('code'))

        b = build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

        return redirect('http://127.0.0.1:8000/profiles/')

PipFreeze公司:

cachetools==3.1.1
certifi==2019.3.9
chardet==3.0.4
Click==7.0
defusedxml==0.6.0
dj-database-url==0.5.0
Django==2.2.1
django-allauth==0.39.1
django-appconf==1.0.3
django-heroku==0.3.1
django-image-cropping==1.2.0
django-multiselectfield==0.1.8
easy-thumbnails==2.6
Flask==1.0.3
google-api-python-client==1.7.9
google-auth==1.6.3
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.3.0
gunicorn==19.9.0
httplib2==0.12.3
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.1
jsonpickle==1.2
MarkupSafe==1.1.1
oauthlib==3.0.1
Pillow==6.0.0
psycopg2==2.8.2
pyasn1==0.4.5
pyasn1-modules==0.2.5
python-dotenv==0.10.2
python3-openid==3.1.0
pytz==2019.1
requests==2.22.0

Tags: djangoselfclientauthapihttpgetrequest
1条回答
网友
1楼 · 发布于 2024-04-26 06:29:54

你使用flow.fetch_token不正确。它的返回值不是credentials对象,而是一组令牌。documentation说明您不应该使用这个:

Returns the obtained tokens. Typically, you will not use return value of this function and instead and use credentials() to obtain a Credentials instance.

你需要做的是:

flow.fetch_token(code=self.request.GET.get('code'))
# The credentials are available in flow.credentials, not the return value
b = build(API_SERVICE_NAME, API_VERSION, credentials=flow.credentials)

相关问题 更多 >