0

I want to retrieve automatically some analytics data about our contents on our Youtube Channel such as views, likes, average view time, subscriptions by videos... So I followed the official documentation of Youtube Analytics, but when I run the python script it keeps telling me to go on a link and to enter the token each time I run the script. The problem is I want to make a script that will download all the data automatically without me entering the token each time it runs. I tried with the API key but the token is still asked.

I read that Youtube Analytics works only with Oauth2, is there a way to get those informations without entering the token manually each time ? If no, could I get these data with the YouTube Data API ?

Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Here is the answer to your question: [How to bypass entering authentication code to authorize my code everytime I use the YouTube Data API v3](https://stackoverflow.com/a/64719550/8327971). That code pertains to any Google API that uses OAuth2. – stvar Dec 24 '20 at 15:06
  • @stvar Unfortunately with this method you have to create a pickle to store the token, but when the token expire you still have to go to the OAuth autorisation link again. Does that mean that we don't have a way directly with the google console API to query Youtube Analytics as we can query Google Analytics just with an API key without clicking on a link to generate a token each time ? – Ali Azzouz Jan 05 '21 at 14:28
  • Let me comment a bit about the function `create_service` defined within my answer that I quoted above (I'm doing it hopping that its works will become more clear). Firstly, do note that the pickle file contains in fact two different tokens: an (long-lived) refresh token and a (short-lived; usually valid for an hour) access token. The refresh token is initialized upon an successful OAuth authentication/authorization flow and once initialized is does not change: `flow = InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes); cred = flow.run_console()`. – stvar Jan 05 '21 at 16:02
  • The access token is refreshed when needed based on the invariant refresh token: `cred.refresh(Request())`. Refreshing an access token is done outside any OAuth flow. The access token is the concrete credentials data that gets passed on to each API call issued through the object `youtube` obtained from `create_service`. Important to acknowledge is the following fact: once you successfully got through an OAuth flow (thus obtaining the refresh token that gets stored withing first pickle file), subsequent calls to `create_service` will not initiate another OAuth flows. – stvar Jan 05 '21 at 16:10
  • That's because the second (third, etc) call to `create_service` will do the following: (1) read in the pickle file; (2) check whether the stored access token expired (refreshing it if needed); (3) save the current credential object to the pickle file. Calling `create_service` the 2nd (3rd, etc) time upon an successful OAth flow, will not execute 'flow = InstalledAppFlow.from_client_secrets_file(...)` and `cred = flow.run_local_server()`. – stvar Jan 05 '21 at 16:23
  • According to the [official docs](https://developers.google.com/youtube/reporting/v1/reports#retrieving-youtube-analytics-reports), (quote): ***Retrieving YouTube Analytics reports**: **Step 1: Retrieve authorization credentials**: All YouTube Reporting API requests must be authorized. The [Authorization guide](https://developers.google.com/youtube/reporting/guides/authorization) explains how to use the OAuth 2.0 protocol to retrieve authorization tokens.* – stvar Jan 05 '21 at 16:26
  • Thus you cannot use an API key for accessing the YouTube Analytics API. Also you must pay attention the have set the proper authorization scopes. – stvar Jan 05 '21 at 16:29

0 Answers0