1

Im trying to use the youtube analytics API. I already have the access token of the channel stored in some db. In Php, you can build the client and just add the access token, client_id, client_secret then use the client to call youtube analytics.

In the python example however, I saw something like this:

flow = flow_from_clientsecrets(
    CLIENT_SECRETS_FILE,
    scope=' '.join(YOUTUBE_SCOPES),
    message=MISSING_CLIENT_SECRETS_MESSAGE)

storage = Storage('%s-oauth2.json' % sys.argv[0])
credentials = storage.get()

if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, data)

http = credentials.authorize(httplib2.Http())

yt.analytics = build(
    YOUTUBE_ANALYTICS_API_SERVICE_NAME,
    YOUTUBE_ANALYTICS_API_VERSION,
    http=http)

It authenticates the user using the browser. I don't need to go by that step since I already have the access_token stored in the db. The question is how to use that access_token in the build() function call so that I can proceed with the query below.

analytics_query_response = yt_analytics.reports().query(
    ids='channel==',
    metrics=options.metrics,
    dimensions=options.dimensions,
    start_date=options.start_date,
    end_date=options.end_date,
    max_results=options.max_results,
    sort=options.sort,
).execute()
Ninz
  • 231
  • 3
  • 11

1 Answers1

2

Unfortunately, the build function doesn't have an access_token parameter. Here are the docs.

You can also create your credentials object like this, though (which may be what you are looking for):

from oauth2client.client import GoogleCredentials
from oauth2client import GOOGLE_TOKEN_URI

access_token = YOUR_TOKEN
token_expiry = None
token_uri = GOOGLE_TOKEN_URI
user_agent = 'Python client library'
revoke_uri = None

credentials = GoogleCredentials( 
    access_token, 
    client_id,
    client_secret, 
    refresh_token, 
    token_expiry,
    token_uri, 
    user_agent,
    revoke_uri=revoke_uri
    )
Daniel
  • 2,345
  • 4
  • 19
  • 36