如何从 json 文件为 YouTube Analytics api 加载客户端凭据?
How to load client credentials from json file for YouTube Analytics api?
我想从朋友的 YouTube 频道中检索获利数据。我使用下面的 python 代码从他那里获取身份验证凭据,然后将其保存到 JSON 文件中。在那个过程中,他必须单击 link 并将密钥发送给我。我想通过保存凭据数据来避免这种情况。我想我已经做到了,但是 我现在如何加载它?
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)
我想从朋友的 YouTube 频道中检索获利数据。我使用下面的 python 代码从他那里获取身份验证凭据,然后将其保存到 JSON 文件中。在那个过程中,他必须单击 link 并将密钥发送给我。我想通过保存凭据数据来避免这种情况。我想我已经做到了,但是 我现在如何加载它?
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)