Search code examples
javascriptgoogle-apiyoutube-apigoogle-api-js-clientyoutube-analytics-api

How to receive data from authorized channel with Youtube Analytics API without being logged into google as the channel owner?


I've been using Youtube Analytics API v2 for retrieving information from youtube channels, it's been working for me when I use it to receive a report from a channel from the user currently authenticated through OAuth2.0, which I used the sample code in their website to do so, now all I need is to receive these reports without being logged in as the owner of the channel which already gave the required permissions to the API, how can I do it?

I've seen that I required the apiKey to do so, I've tried the gapi.client.setApiKey("API_KEY") code on every place in the code but I didn't get any results, maybe I'm missing something else, I've picked the sample code from youtube analytics website and changed the "ids" parameter from "channel==MINE" to "channel==PREVIOUSLY_AUTHORIZED_CHANNEL_ID" and now I get error 403(forbidden) when I try to access it without being on the respective Google account.

<script src="https://apis.google.com/js/api.js"></script>
<script>
  function authenticate() {
      gapi.client.setApiKey("<API_KEY>");
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/yt-analytics.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    return gapi.client.load("https://youtubeanalytics.googleapis.com/$discovery/rest?version=v2")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.youtubeAnalytics.reports.query({
      "ids": "channel==<PREVIOUSLY_AUTHORIZED_CHANNEL_ID>",
      "startDate": "2017-01-01",
      "endDate": "2017-12-31",
      "metrics": "views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained",
      "dimensions": "day",
      "sort": "day"
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: '17857976174-df6g4g2kajnh8mpoebk0o10n4o7ddvb8.apps.googleusercontent.com'});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

Solution

  • now all I need is to receive these reports without being authenticated as the owner of the channel, how can I do it?

    You cant. YouTube analytics data is private user data. Private data requires the users consent in order to access it. The only way you are going to be able to access that is with the users consent.

    API keys are used to access public data. Like YouTube search for public youtube videos. Some public Google calendars and public google drive files.