Joh*_* D. 6 php youtube-api google-api-php-client youtube-analytics
我可以通过API Explorer成功向Youtube Analytics API发出请求.我的代码正在尝试使用Google PHP客户端库,特别是Google_Service_YouTubeAnalytics类.不幸的是,这个课程没有文档.
我在客户端上设置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'
);
Run Code Online (Sandbox Code Playgroud)
您需要添加 Google 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}
Run Code Online (Sandbox Code Playgroud)
我也不确定“%2Cuniques”=>“uniques”是否是有效的指标。
您可以使用 Google 自动化工具生成有效链接。
https://developers.google.com/youtube/analytics/v1/