2014-10-17 92 views
6

我能夠通過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' 
); 
+0

此外,我在我的代碼中使用Google Analytics客戶端庫,並且工作正常。我以幾乎相同的方式使用代碼。 Uggh。 – 2014-10-17 17:54:07

+0

您的應用程序是否實際上將字符串「channel == CHANNEL_ID」作爲ids參數的值發送,或者在您將錯誤代碼粘貼到SO中時進行修改? – jlmcdonald 2014-10-18 05:24:08

+0

不,我改變了這個,以便在這篇文章中隱藏我的實際頻道ID。在我的代碼中,這是我的頻道ID值。 – 2014-10-20 16:20:31

回答

0

您需要添加一個谷歌API密鑰

https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel_ID&start-date=2014-09-01&end- 
date=2014-10-01&metrics=views%2Cuniques&key={YOUR_API_KEY} 

而且我不知道,如果 「%2Cuniques」=> 「唯一身份」是有效的度量。

您可以使用Google自動化工具生成有效的鏈接。

https://developers.google.com/youtube/analytics/v1/

1

你正在一個不支持查詢,這也可以使用意見 & 的唯一身份沒有提供尺寸。 您可以在Youtube's Analytics API Reference中查看。

嘗試將其添加一個維度,例如天,它會工作:

$youtubeService = new \Google_Service_YouTubeAnalytics($client); 
$resp = $youtubeService->reports->query(
    self::CHANNEL_ID, 
    '2014-09-01', 
    '2014-09-05', 
    'views,uniques', 
    array('dimensions' => 'day') 
); 

該查詢會得到類似迴應:

200 OK 

- Show headers - 

{ 
"kind": "youtubeAnalytics#resultTable", 
"columnHeaders": [ 
    { 
    "name": "day", 
    "columnType": "DIMENSION", 
    "dataType": "STRING" 
    }, 
    { 
    "name": "views", 
    "columnType": "METRIC", 
    "dataType": "INTEGER" 
    }, 
    { 
    "name": "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 
    ] 
] 
} 

Google's APIs Explorer是爲了一個非常有用的工具測試你的查詢。


有關文檔的目的,你可以看看源代碼和類本身,他們是很好的記錄和「也許」不言自明。


一種更新方法是使請求使用OAuth 2.0協議的用於授權訪問這個API。
谷歌提供了一個驚人的資源來嘗試這一切的東西:OAuth 2.0 Playground

基本上,你需要獲得一個訪問令牌和刷新令牌之前到期時應用它。

$client = new \Google_Client(); 
$client->setApplicationName(self::APP_NAME); 

// set some stuff 
$client->setClientId(self::CLIENT_ID); 
$client->setClientSecret(self::CLIENT_SECRET); 

// Set oAuth info 
$client->addScope(\Google_Service_YouTubeAnalytics::YT_ANALYTICS_READONLY); 
$client->setAccessToken($accessToken); 

// Check if token is expired 
if ($client->isAccessTokenExpired()) { 
    $client->refreshToken($refreshToken()); 
    $newToken = $client->getAccessToken(); 

    $authObj = json_decode($newToken); 
    if (!is_object($authObj)) { 
     throw new \Google_Auth_Exception('Error on updating oAuth tokens'); 
    } 

    //update tokens 
    //...    
} 

$youtubeService = new \Google_Service_YouTubeAnalytics($client); 

希望它有幫助!