【发布时间】:2019-05-31 11:36:36
【问题描述】:
我想连接到 Youtube 分析,它将数据写入数据库可以是 mysql 或 mariodb。 不幸的是,我遇到了自动授权问题,我收到了授权错误。
A service error occurred: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }
没有授权可能是什么原因?
Skorzystałem z kodu Harvey Connor:YouTube Analytics API php Samples
set_include_path(get_include_path() . PATH_SEPARATOR . '/home/google-api-php-client/src');
require_once('Google/autoload.php');
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAccessType("offline");
$client->setScopes(array('https://www.googleapis.com/auth/youtube.force-ssl', 'https://www.googleapis.com/auth/youtubepartner-channel-audit', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly','https://www.googleapis.com/auth/youtubepartner'));
$client->setDeveloperKey($key);
$analytics = new Google_Service_YouTubeAnalytics($client);
$ids = 'channel==' . $channel_url . '';
$end_date = date("Y-m-d");
$start_date = date('Y-m-d', strtotime("-30 days"));
$optparams = array(
'dimensions' => 'day',
);
$metric = 'views';
try{
$api = $analytics->reports->query($ids, $start_date, $end_date, $metric, $optparams);
foreach ($api->rows as $r) {
$date = $r[0];
$views = $r[1];
$stmt = $db->prepare("INSERT INTO test (date,views,channel_url) VALUES (:date,:views,:channel_url)");
$stmt->execute([':date' => $date, ':views' => $views, ':channel_url' => $channel_url]);
}
}catch (Google_Service_Exception $e) {
echo sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
我的想法现在用完了
【问题讨论】:
-
请阅读How to Ask。你需要问我们一个实际的问题,而不是仅仅说你“有问题”,而没有对这个问题到底是什么做任何真正的解释。
-
很抱歉我已经更正了
-
那么您实际上是在该代码中的哪个位置尝试授权...?
标签: php mysql youtube-api youtube-analytics-api