youtube-api - 通过 API 回复 youtube 评论的范围不足
问题描述
我想通过 PHP API 自动回复 youtube 评论。
我得到“请求的身份验证范围不足”,我是通过 cURL 进行的,并且我之前已经进行了身份验证....
这是代码:
// Call set_include_path() as needed to point to your client library.
require_once __DIR__ . '/../vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__ .'/../');
$dotenv->load();
session_start();
require __DIR__ . '/../includes/db.php';
require __DIR__ . '/../includes/api_calls.php';
$API_KEY = $_ENV['API_KEY'];
$OAUTH2_CLIENT_ID = $_ENV['CLIENT_ID'];
$OAUTH2_CLIENT_SECRET = $_ENV['CLIENT_SECRET'];
$OAUTH2_SCOPES = array(
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.force-ssl'
);
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setDeveloperKey($API_KEY);
$client->setScopes($OAUTH2_SCOPES);
$redirect = filter_var('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$token = $_SESSION['token']['access_token'];
$client->setAccessToken($token);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
$commentId = trim($_POST['commentId']);
$textOriginal = "Thanks!! ";
$insertResponse = postYoutubeCommentReply($commentId , $textOriginal, $token , $API_KEY);
echo json_encode($insertResponse);
} catch (Google_Service_Exception $e) {
$result['status'] = 'error';
$result['error_message'] = htmlspecialchars($e->getMessage());
echo json_encode($result);
} catch (Google_Exception $e) {
$result['status'] = 'error';
$result['error_message'] = htmlspecialchars($e->getMessage());
echo json_encode($result);
}
}
function postYoutubeCommentReply($commentId , $text, $token, $API_KEY) {
$comment['snippet']['textOriginal'] = "Thanks!!"
$comment['snippet']['parentId'] = $commentId;
$call_url = 'https://youtube.googleapis.com/youtube/v3/comments?part=id%2C%20snippet&key=' . $API_KEY;
$insertResponse = youtube_api_call_oauth($call_url, $token, $comment);
return $insertResponse;
}
function youtube_api_call_oauth($url, $access_token, $data = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$headers = [
'Authorization: Bearer ' . $access_token,
'Accept: application/json',
'Content-Type: application/json'
];
if (!is_null($data) ){
curl_setopt($ch, CURLOPT_POST, 1);
$data_json = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$html = curl_exec($ch);
if (curl_error($ch))
die(curl_error($ch));
// Get the status code
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($html, true);
}
我试着做了几天我找不到路......
谢谢你的帮助!..................................................... ..................................................... ............
解决方案
推荐阅读
- c++ - 如何使用内联汇编通过 MOVNTI 指令将数据写入可变内存地址?
- excel-formula - 如何将此 COUNTIFS 转换为 Power Query 以获得更好的性能目的?
- python - 在绘图命令之外调用 mplfinance 绘图不保存
- tensorflow - 修改隐藏层的梯度并反向传播修改后的梯度以找到剩余层的梯度
- delphi - 如何使用 TActionList 中的操作?
- javascript - 检查元素是否是javascript中的对象
- geoserver - GeoServer 播种平铺层时出现未经授权的错误
- python - 数字的索引二进制类型
- flutter - Flutter - 列表上的等待/异步 - 为什么这只在不使用声明时才有效?
- python - 在 Python 中连接两个列表的元素