此功能用于从 Google 获取经过身份验证的服务。后
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)
Run Code Online (Sandbox Code Playgroud)
但我想使用 Refresh Token 自动进行身份验证,而无需打开浏览器。因此在上面的函数中添加一些代码来保存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)
Run Code Online (Sandbox Code Playgroud)
好的,那么在拥有刷新令牌后,如何使用它?我试图替换刷新令牌="ABCDEF456"但它不起作用
def get_authenticated_service():
return build(API_SERVICE_NAME, API_VERSION, credentials="ABCDEF456")
Run Code Online (Sandbox Code Playgroud)