Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google API流每次刷新令牌都需要获得许可_Python_Python 3.x_Google Api_Google Calendar Api_Google Oauth - Fatal编程技术网

Python Google API流每次刷新令牌都需要获得许可

Python Google API流每次刷新令牌都需要获得许可,python,python-3.x,google-api,google-calendar-api,google-oauth,Python,Python 3.x,Google Api,Google Calendar Api,Google Oauth,使用Python3.6,请求==2.22.0 正在尝试使用GoogleAPI,尤其是 我可以使用以下url生成身份验证代码: url = ( 'https://accounts.google.com/o/oauth2/v2/auth?' 'scope={scope}' 'response_type=code&' 'redirect_uri={redirect_uri}&' 'client_id={client_id}&' '

使用Python3.6,请求==2.22.0

正在尝试使用GoogleAPI,尤其是

我可以使用以下url生成身份验证代码:

url = (
    'https://accounts.google.com/o/oauth2/v2/auth?'
    'scope={scope}'
    'response_type=code&'
    'redirect_uri={redirect_uri}&'
    'client_id={client_id}&'
    'access_type=offline'.format(
        redirect_uri=redirect_uri,
        client_id=client_id,
        scope=scope,
    )
)
我现在使用的重定向uri是https://google.com,并在我生成的开发者应用程序、授权重定向URI部分和OAuth同意设置页面/选项卡下的授权域部分中注册

一旦我将生成的url粘贴到浏览器中,我会得到一个代码,可以提取并用于下一次调用:

data = {
    'client_id': client_id,
    'client_secret': client_secret,
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': redirect_uri,
}
url = 'https://oauth2.googleapis.com/token'

response = requests.post(
    url,
    data=data,
    headers={
        'Content-Type': 'application/x-www-form-urlencoded',
    },

print(response)
print(response.json())
输出:

<Response [200]>
{tokens dictionary} <-- more downstream
接下来,我添加了我感兴趣的实际作用域,并确保在开发者应用程序中添加它们:

https://www.googleapis.com/auth/calendar.events+https://www.googleapis.com/auth/calendar.readonly
我得到的结果是:

{'access_token': '******', 'expires_in': 3595, 'scope': 'https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly', 'token_type': 'Bearer'}
没有刷新令牌?我特别要求它刷新访问令牌

然后我阅读并添加了&prompt=同意上述代码授予URL:https://accounts.google.com/o/oauth2/v2/auth

现在我得到:

{'access_token': '******', 'expires_in': 3600, 'refresh_token': '******', 'scope': 'https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly', 'token_type': 'Bearer'}
我现在有刷新令牌,但这是唯一的方法吗? 如果用户最终将再次通过该流,它将强制另一个同意页面流-在已经给出初始同意后,不应要求该页面流

有没有办法每次都在没有明确同意的情况下获取刷新令牌

有没有办法每次都在没有明确同意的情况下获取刷新令牌

否。在用户同意脱机访问的情况下,第一次返回刷新令牌。 谷歌假设你已经保存了它,并且不需要另一个。撤销用户访问权限并再次请求访问权限。您应该获得一个刷新令牌。或者,发送请求提示符将请求用户再次授予您脱机访问权限,您将再次获得新的刷新令牌

{'access_token': '******', 'expires_in': 3600, 'refresh_token': '******', 'scope': 'https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly', 'token_type': 'Bearer'}