1

So I make a request to youtube analytics that has the following scopes:

    'scope'        => 'https://www.googleapis.com/auth/yt-analytics.readonly https://gdata.youtube.com https://www.googleapis.com/auth/userinfo.profile'

Using my access token I try to get a list of channel subscribers from the last month, like this:

 $command = 'curl -H "Authorization: Bearer ' . $access_token  . '"https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2016-07-01&end-date-2016-08-31&metrics=views&dimensions=day&sort=day';
        exec($command, $result);

Even though I succeed in getting the access token and saving the user's credentials, what I get from this request is this:

array(0) { }

Without any kind of error. Does anyone have any idea why this might happen. If you require more details just ask me. I am on the clock with this problem so I really need some help fast. Any help is welcomed. Thank you all for your time! This is what I get from making a request in APIs Explorer:

{
 "kind": "youtubeAnalytics#resultTable",
 "columnHeaders": [
  {
   "name": "day",
   "columnType": "DIMENSION",
   "dataType": "STRING"
  },
  {
   "name": "views",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  }
 ]
}
Alan
  • 213
  • 1
  • 13
  • Can you provide the same data using the API Explorer? I tried it out but I can't get any data since my account doesn't have a YT channel. But, based on the response you have - its different from the [response body](https://developers.google.com/youtube/analytics/v1/#response) that's specified on the YouTube Analytics API – adjuremods Sep 14 '16 at 10:18
  • @adjuremods the response body is supposed to be saved in ["data"]=> (the rest of the array is from my project) – Alan Sep 14 '16 at 11:20
  • I do not know how to use api explorer, I'll check it out now – Alan Sep 14 '16 at 11:21
  • 1
    I executed it using the same parameters in api explorer I updated my question with the response – Alan Sep 14 '16 at 11:40
  • I edited how my response actually looks like – Alan Sep 14 '16 at 13:53
  • The updated response you provided is similar to mine, I believe the data will be on the `rows` element. I have no data since I don't have a channel. Can you try using other dimensions/metrics to see if it will still return empty? Does the channel you set have any views? – adjuremods Sep 14 '16 at 14:25
  • I did try and I do have views, this is why I don't know what is wrong – Alan Sep 14 '16 at 15:11

1 Answers1

0

I have found the solution to my problem. In my request end-date had a typo:

end-date-2016-08-31

The correct version looks like this:

end-date=2016-08-31

And the url I was calling had to have quotation marks. So my correct call looks like this:

 $command = 'curl -H "Authorization: Bearer ' . $access_token  . '" "https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=' . date('Y-m-d', strtotime('-31 days')) . '&end-date=' . date('Y-m-d', strtotime('today')). '&metrics=subscribersGained%2CsubscribersLost&dimensions=day&sort=day"';

Thank you for your time!

Alan
  • 213
  • 1
  • 13