All Requests to Youtube Analytics API via Google PHP Library Results in 400 Bad Request -
I am able to successfully request a YouTube Analytics API via API Explorer. My code is trying to use the Google PHP client library, specifically the Google_Service_YouTubeAnalytics class. Unfortunately, there is no document on this class.
I am setting the ID and identity authentication on the client. I fully believe that it is working correctly, because if I do something wrong to private key that I know wrong, then I get it:
{"code" : 400, "error": "Error refreshing OAuth2 token: message: '{\ n \" error \ ": \" invalid_grant \ "\ n}'"}
But when I insert the right private key, I get the following response:
{"code": 400, "error": "https: / www.googleapis.com \ / youtube Error getting / analytics / v1 "\ / Reports? Id = channel% 3D% 3DCHANNEL_ID and start date = 2014-09-01 and end date = 2014-09-05 and metrics = visible% 2 key: (400) Invalid query. The queries did not follow the expectations. "}
It does not tell me what is invalid about the query (which would be incredibly useful), so I do not know what I'm doing wrong. The help is appreciated.
This is my code that requests:
$ client = new \ Google_Client (); $ Customer & gt; setApplicationName :: APP_NAME); // Set some goods $ client- gt; setClientId (auto :: CLIENT_ID); $ client- & gt; set client alert (auto :: CLIENT_SECRET); $ Client- & gt; set unclaimed Credentials (new \ Google_Auth_AssertionCredentials (auto :: CRED_ID, ["https://www.googleapis.com/auth/youtube.readonly", 'https://www.googleapis.com/auth/yt-alytics.readonly'] , Auto :: youtube ()); $ YoutubeService = new \ Google_Service_YouTubeAnalytics ($ customer); $ Resp = $ youtubeService- & gt; Report- & gt; Query (auto :: CHANNEL_ID, '2014-09-01', '2014-09-05', 'View, Unique');
Supported queries are not being created, it is not possible to use view & amp; No dimensions were provided with uniques . You can test it by adding a dimension like
it day and it will work:
$ youtubeService = new \ Google_Service_YouTubeAnalytics ($ Client); $ Resp = $ youtubeService- & gt; Report-> Query (auto :: CHANNEL_ID, '2014-09-01', '2014-09-05', 'View, Uniars', array ('dimension' => 'days'));
The query will receive a similar response:
200 OK - Show headers - {"Type": "youtubeanalytics # resulttable", "columnHeaders" "" "{{Name}:" data type "," datatype "," data type ":" STRING "}, {" name ":" view "," column type ":" INTEGER "": "[": "Uniques", "columnType": "METRIC", "dataType": "INTEGER"}], "rows": [["2014-09-04", 1250, 621] ["2014 -09-05 ", 1265, 577], [" 2014-09-03 ", 1255, 557], [" 2014-09-01 ", 1076, 532], [" 2014-09-02 ", 1182, 570]]}
is a very useful tool to test your questions.
For documenting purposes, you can see the source code and the classes themselves, they are very well documented and "probably" are self explanatory.
To authorize access, request this API using the oAuth 2.0 protocol .
Google offers an amazing resource to try out all this stuff:
Basically, you need to get a free token and its fresh token. $ Customer & gt; SetApplicationName (auto :: APP_NAME); // Set some goods $ client- gt; SetClientId (auto :: CLIENT_ID); $ Client- & gt; Set client alert (auto :: CLIENT_SECRET); // set-up information $ client- & gt; Aspoc (\ Google_Service_YouTubeAnalytics :: YT_ANALYTICS_READONLY); $ Customer & gt; SetAccessToken ($ accessToken); // Check if the token has expired ($ client- & gt; exxpecton exp ()) {$ client- & gt; Refresh Talkone ($ Fresh Tokens ()); $ NewToken = $ client- & gt; GetAccessToken (); $ AuthObj = json_decode ($ newToken); If (! Is_object ($ authObj)) {new \ Google_Auth_Exception ('Error in updating Auto Touches'); } // update token // ...} $ youtubeService = new \ Google_Service_YouTubeAnalytics ($ client); $ client = new \ Google_Client ();
Hope it helps!
Comments
Post a Comment