我能夠通過API Explorer成功向Youtube Analytics API發送請求。我的代碼試圖使用Google PHP客戶端庫,特別是Google_Service_YouTubeAnalytics類。不幸的是,這個類沒有任何文檔。所有通過谷歌PHP請求的YouTube分析API PHP庫的結果請求400請求
我在客戶端設置ID和斷言憑證。我相當有信心,這是正常工作,因爲如果我更改私鑰的東西,我知道是不正確的,我得到:
{"code":400,"error":"Error refreshing the OAuth2 token, message: '{\n \"error\" : \"invalid_grant\"\n}'"}
但是當我插入正確的私鑰,我得到以下回應:
{"code":400,"error":"Error calling GET https:\/\/www.googleapis.com\/youtube\/analytics\/v1\/reports?ids=channel%3D%3DCHANNEL_ID&start-date=2014-09-01&end-date=2014-09-05&metrics=views%2Cuniques: (400) Invalid query. Query did not conform to the expectations."}
它沒有告訴我什麼是對查詢(這將是難以置信的幫助)無效的,所以我不知道我可以做的不正確。任何幫助表示讚賞。
這裏是我的代碼發出請求:
$client = new \Google_Client();
$client->setApplicationName(self::APP_NAME);
// set some stuff
$client->setClientId(self::CLIENT_ID);
$client->setClientSecret(self::CLIENT_SECRET);
$client->setAssertionCredentials(new \Google_Auth_AssertionCredentials(
self::CRED_ID,
[
"https://www.googleapis.com/auth/youtube.readonly",
'https://www.googleapis.com/auth/yt-analytics.readonly'
],
self::youtubeKey()
));
$youtubeService = new \Google_Service_YouTubeAnalytics($client);
$resp = $youtubeService->reports->query(
self::CHANNEL_ID,
'2014-09-01',
'2014-09-05',
'views,uniques'
);
此外,我在我的代碼中使用Google Analytics客戶端庫,並且工作正常。我以幾乎相同的方式使用代碼。 Uggh。 – 2014-10-17 17:54:07
您的應用程序是否實際上將字符串「channel == CHANNEL_ID」作爲ids參數的值發送,或者在您將錯誤代碼粘貼到SO中時進行修改? – jlmcdonald 2014-10-18 05:24:08
不,我改變了這個,以便在這篇文章中隱藏我的實際頻道ID。在我的代碼中,這是我的頻道ID值。 – 2014-10-20 16:20:31