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)
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")