小编Ped*_*oen的帖子

在 python 上为 Oauth2 设置安全 https 连接

我正在尝试使用 google people API 自动在我们的工作电话上创建一些联系人。但为了使用 people API,我需要使用 OAuth 2,并且我使用以下代码。我转到授权 URL,获取授权响应,但是当我运行令牌行时,出现错误:

InsecureTransportError: (insecure_transport) OAuth 2 MUST utilize https.
Run Code Online (Sandbox Code Playgroud)

代码如下:

import json
with open(r'C:\Users\Pedro\Desktop\client_id.json') as abb:
    ab = json.load(abb)



client_id = ab['installed']['client_id']
redirect_uri = ab['installed']['redirect_uris'][0]
client_secret = ab['installed']['client_secret']

from requests_oauthlib import OAuth2Session


scope = ['https://www.googleapis.com/auth/contacts']
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri,
                          scope=scope)
authorization_url, state = oauth.authorization_url(
        'https://accounts.google.com/o/oauth2/auth',
        # access_type and prompt are Google specific extra
        # parameters.
        access_type="offline", prompt="select_account")

#authorization_response = input('Enter the full callback URL')
authorization_response = r'RESPONSE'


token = oauth.fetch_token(
        'https://accounts.google.com/o/oauth2/token',
        authorization_response=authorization_response, …
Run Code Online (Sandbox Code Playgroud)

python https oauth-2.0 python-3.x python-requests

4
推荐指数
1
解决办法
2893
查看次数

标签 统计

https ×1

oauth-2.0 ×1

python ×1

python-3.x ×1

python-requests ×1