如何在Python中使用刷新令牌Google API? [英] How to use Refresh Token Google API in Python?

查看:227
本文介绍了如何在Python中使用刷新令牌Google API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此功能用于从Google获得经过身份验证的服务.之后

This function is to get authenticated service from Google. After

        API_SERVICE_NAME = 'youtubereporting'
        API_VERSION = 'v1'
        CLIENT_SECRETS_FILE = "client_secret_929791903032-hpdm8djidqd8o5nqg2gk66efau34ea6q.apps.googleusercontent.com.json"
        SCOPES = ['https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
        def get_authenticated_service():
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
            credentials = flow.run_local_server()
            return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

但是我想使用刷新令牌来自动进行身份验证,而无需打开浏览器.因此,在上面的函数中添加一些代码以保存刷新令牌:

But I want to use Refresh Token to automatically authenticate without opening a browser. Therefore add some codes into the function above to save Refresh Token:

def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_local_server()
    with open('refresh.token', 'w+') as f:
        f.write(credentials._refresh_token)
    print('Refresh Token:', credentials._refresh_token)
    print('Saved Refresh Token to file: refresh.token')
    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

好的,那么在拥有刷新令牌后,如何使用它?我试图替换刷新令牌="ABCDEF456",但是它不起作用

Okay, so after having the Refresh Token, How to use it? I tried to replace the refresh token ="ABCDEF456" but it does not work

def get_authenticated_service():
    return build(API_SERVICE_NAME, API_VERSION, credentials="ABCDEF456")

推荐答案

要刷新访问令牌,您可以调用Google OAuth端点,并传入三个参数:

To refresh an Access Token, you call the Google OAuth endpoint passing in three parameters:

  • 客户ID
  • 客户秘密
  • 刷新令牌

这可以通过简单的HTTP POST请求非常简单地完成.

This can be done very simply with a simple HTTP POST request.

以下是使用curl的示例:

Here is an example using curl:

set REFRESH_TOKEN=REPLACE_WITH_REFRESH_TOKEN

curl ^
--data client_id=%CLIENT_ID% ^
--data client_secret=%CLIENT_SECRET% ^
--data grant_type=refresh_token ^
--data refresh_token=%REFRESH_TOKEN% ^
https://www.googleapis.com/oauth2/v4/token

在Python中使用请求库:

In Python using the requests library:

// Call refreshToken which creates a new Access Token
access_token = refreshToken(client_id, client_secret, refresh_token)

// Pass the new Access Token to Credentials() to create new credentials
credentials = google.oauth2.credentials.Credentials(access_token)

// This function creates a new Access Token using the Refresh Token
// and also refreshes the ID Token (see comment below).
def refreshToken(client_id, client_secret, refresh_token):
        params = {
                "grant_type": "refresh_token",
                "client_id": client_id,
                "client_secret": client_secret,
                "refresh_token": refresh_token
        }

        authorization_url = "https://www.googleapis.com/oauth2/v4/token"

        r = requests.post(authorization_url, data=params)

        if r.ok:
                return r.json()['access_token']
        else:
                return None

注意:如果最初在授权请求期间请求,此代码还将返回刷新的ID令牌.

Note: This code will also return a refreshed ID Token if originally requested during the authorization request.

这篇关于如何在Python中使用刷新令牌Google API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆