我正在尝试使用带有服务帐户的API获取一些YouTube Analytics报告,以便执行一些CRON作业。我已经解决了NTP服务器同步的常见问题,显然,我已经正确设置了API控制台。
<?php
ini_set('display_errors', 1);
require_once 'gapi/src/Google_Client.php';
require_once 'gapi/src/contrib/Google_YouTubeAnalyticsService.php';
// Define constants.
const CLIENT_ID = '************************.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '************************@developer.gserviceaccount.com';
const KEY_FILE = '************************-privatekey.p12';
const MY_CHANNEL = '************************';
$client = new Google_Client();
$client->setApplicationName("YouTube Analytics API Test");
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array("https://www.googleapis.com/auth/yt-analytics.readonly"),
$key)
);
$client->setClientId(CLIENT_ID);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$token = $client->getAccessToken();
$youtubeAnalyticsService = new Google_YouTubeAnalyticsService($client);
$optparam = array("dimensions" => "country", "max-results" => "10");
$reports = $youtubeAnalyticsService->reports->query("channel==" . MY_CHANNEL, "2013-09-01", "2013-09-25", "views", $optparam);
echo $reports;
?>
给我回复了这条消息:
Fatal error: Uncaught exception 'Google_ServiceException' with message
'Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DmychannelID&start-date=2013-09-01&end-date=2013-09-25&metrics=views&dimensions=country&max-results=10: (500) Unknown error occurred on the server.' in /mydomain/gapi/src/io/Google_REST.php:66
Stack trace:
0 /mydomain/gapi/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest))
1 /mydomain/gapi/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest))
2 /mydomain/gapi/src/contrib/Google_YouTubeAnalyticsService.php(48): Google_ServiceResource->__call('query', Array)
3 /mydomain/myfile.php(45): Google_ReportsServiceResource->query('channel==...', '2013-09-01', '2013-09-25', 'views', Array)
4 {main} thrown in /mydomain/gapi/src/io/Google_REST.php on line 66
有什么建议吗?提前谢谢大家!