Python 如何从YouTube分析api的json文件加载客户端凭据?
我想从朋友的YouTube频道检索货币化数据。我使用了下面的python代码从他那里获取身份验证凭据,然后将其保存到JSON文件中。在这个过程中,他必须单击一个链接并将密钥发送给我。我希望通过保存凭据数据来避免这种情况。我想我已经这样做了,但是现在如何加载它呢?Python 如何从YouTube分析api的json文件加载客户端凭据?,python,google-oauth,youtube-analytics-api,Python,Google Oauth,Youtube Analytics Api,我想从朋友的YouTube频道检索货币化数据。我使用了下面的python代码从他那里获取身份验证凭据,然后将其保存到JSON文件中。在这个过程中,他必须单击一个链接并将密钥发送给我。我希望通过保存凭据数据来避免这种情况。我想我已经这样做了,但是现在如何加载它呢? import json import os import google.oauth2.credentials import google_auth_oauthlib.flow from googleapiclient.discovery
import json
import os
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly']
API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'client_secret_dota2rapier_youtube_analytics_api.json'
CLIENT_CREDENTIALS_FILE = 'credentials.json'
root = 'C:\\test\\'
os.chdir(root)
def get_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
#SAVING CREDENTIALS DATA
creds_data = {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes
}
save = True
if save:
del creds_data['token']
with open('credentials.json', 'w') as outfile:
json.dump(creds_data, outfile)
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def execute_api_request(client_library_function, **kwargs):
response = client_library_function(
**kwargs
).execute()
print(response)
if __name__ == '__main__':
# Disable OAuthlib's HTTPs verification when running locally.
# *DO NOT* leave this option enabled when running in production.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
youtubeAnalytics = get_service()
execute_api_request(
youtubeAnalytics.reports().query,
ids='channel==UC0NM4tKT5s9szqnK3jp6dEw',
startDate='2018-12-20',
endDate='2018-12-30',
metrics='views,likes,estimatedRevenue',
dimensions='day',
sort='day'
)
我只需打开文件并将json转储到字典中
def read_config(config):
# config is the name/path to your config file
with open(config, 'r') as infile:
config = json.load(infile)
return(config)
config = read_config("yourfile.json")
id, secret = config["client_id"], config['client_secret"]
我自己解决了。我只需要使用以下代码创建凭据对象:
credentials = google.oauth2.credentials.Credentials.from_authorized_user_file(CLIENT_CREDENTIALS_FILE)
这只是返回json文件的一些内容。我需要创建一个凭据对象。是的,这就是您要求的“我想通过保存凭据数据来避免这种情况。我想我已经这样做了,但是现在如何加载它?”您没有提到凭据对象。我很高兴看到您找到了有关如何创建对象的解决方案:)。请检查是否可以使用保存的刷新令牌刷新访问令牌。我如何检查?无论如何,我现在多次使用凭证来成功地对API执行不同的查询。你的意思是它可能很快过期吗?是的,凭据过期了。通常在3600秒之后。我不是100%确定,但我认为您刷新访问令牌时会遇到问题,因为您的回调url将不同-例如,它必须与Google控制台中的客户端ID/客户端机密匹配。出于安全原因,我很想找到确切的答案,并进一步了解我的OAuth体验。今天,大约20小时后,我再次测试了它。仍然有效感谢您回来回答您自己的问题@AndersLunde-对于Youtube数据API的类似情况,它也派上了用场。在文档中的任何地方都找不到这个,已经搜索了几个小时。与其构建您自己的JSON,
凭据
实例提供了一个to_JSON()
,我相信它也会做同样的事情: