You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm following the example (Google Analytics) to get my credentials and build my service object with the following code and the python SDK:
import argparse
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
DISCOVERY_URI = 'https://analyticsreporting.googleapis.com/$discovery/rest'
CLIENT_SECRETS_PATH = 'google_analytics/client_secrets_R.json' # Path to client_secrets.json file.
def authenticate_ga_api():
"""Initializes the analyticsreporting service object.
Returns:
analytics an authorized analyticsreporting service object.
"""
# Parse command-line arguments.
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser])
flags = parser.parse_args([])
# Set up a Flow object to be used if we need to authenticate.
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH, scope=SCOPES,
message=tools.message_if_missing(CLIENT_SECRETS_PATH))
'''Prepare credentials, and authorize HTTP object with them.
If the credentials don't exist or are invalid run through the native client
flow. The Storage object will ensure that if successful the good
credentials will get written back to a file. '''
storage = file.Storage('analyticsreporting.json')
credentials = storage.get()
if credentials is None or credentials.invalid: # Try to get from another auth source.
storage = file.Storage('ga_token.json')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http=httplib2.Http())
# Build the service object.
analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
return analytics
It works, I get my credentials and it return my "analytics" which works.
But I want to get my credentials from a dictionary (from a DB), not the json.
How I could do that? I can't use the file.Storage('ga_token.json') and storage.get()
I tried: credentials = google.oauth2.credentials.Credentials(**ga_token)
But the credentials created are not the correct type and I have the error: AttributeError: 'Credentials' object has no attribute 'authorize'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm following the example (Google Analytics) to get my credentials and build my service object with the following code and the python SDK:
It works, I get my credentials and it return my "analytics" which works.
But I want to get my credentials from a dictionary (from a DB), not the json.
My dict would be like:
``{"access_token": "ya29.XXXXXXXXXXXXXX-9vw0163", "refresh_token": "1//0fu-XXXXXXXXXXXXXXF-XXXXX", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "000000000000-XXXXX.apps.googleusercontent.com", "client_secret": "GOXXX-XXXXXXXX", "scopes": ["https://www.googleapis.com/auth/analytics.readonly"]}
How I could do that? I can't use the
file.Storage('ga_token.json')
andstorage.get()
I tried:
credentials = google.oauth2.credentials.Credentials(**ga_token)
But the credentials created are not the correct type and I have the error:
AttributeError: 'Credentials' object has no attribute 'authorize'
How I could do that?
Best regards, Diego
Beta Was this translation helpful? Give feedback.
All reactions