python Google OAuth2流程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Google OAuth2流程相关的知识,希望对你有一定的参考价值。

import requests

base_url = 'https://accounts.google.com/o/oauth2/auth'

response = requests.get(base_url, params={
    'client_id': '[client_id]',
    'redirect_uri': 'http://localhost:8000',
    'response_type': 'code',
    'scope': 'profile',
})
print(response.url)

base_url = 'https://accounts.google.com/o/oauth2/token'

response = requests.post(base_url, data={
    'grant_type': 'authorization_code',
    'redirect_uri': 'http://localhost:8000',
    'client_id': '[client_id]',
    'client_secret': '[client_secret]',
    'code': '[code]',
})
if response.status_code != 200:
    print('Rerun auth')
data = response.json()
access_token = data.get('access_token')
print('Access token:', access_token)

base_url = 'https://www.googleapis.com/oauth2/v1/userinfo'
response = requests.get(base_url, params={
    'access_token': access_token,
    'alt': 'json'
})
print(response.url)
print(response.json())

以上是关于python Google OAuth2流程的主要内容,如果未能解决你的问题,请参考以下文章