gpt4 book ai didi

php - Google Youtube API 插入评论总是返回错误响应 403 - "Insufficient Permission"- 域 "global"

转载 作者:行者123 更新时间:2023-12-03 05:24:10 27 4
gpt4 key购买 nike

我遵循了文档指南中的文档和代码示例,即,此处:https://developers.google.com/youtube/v3/docs/commentThreads/insert但是当我执行脚本时,它总是返回一个错误代码的响应:403 message: "Insufficient Permission"

这是完整的响应对象:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}

我花了很多时间调试和上网寻找解决问题的方法,但没有找到任何资源来解决这个问题。如果有人能给我一些提示或解决方案,或者告诉我是否遗漏了什么,我将不胜感激。

附言我还在我的应用程序中使用了其他 API 方法,例如喜欢视频、订阅 channel 、使用 Google 登录,所有这些都运行良好。不明白为什么这个插入评论不起作用。

任何帮助将不胜感激。谢谢。

这是我用于调用 API 的 API 脚本。

<?php    
// filename: api.php
class GoogleApi {

private $client_id;
private $client_secret;
private $redirect_uri;

private $client; // stored client instance
private $yt_service; // youtube service instance for making all api calls

public function __construct($client_id, $client_secret, $redirect_uri, $token = '') {
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->redirect_uri = $redirect_uri;

// create google client instance
$this->client = $this->getClient();

if(!empty($token)) {
// set / refresh access token
$this->setRefreshAccessToken($token);
// Define an object that will be used to make all API requests.
$this->yt_service = new Google_Service_YouTube($this->client);
}
}

// Get a google client instance
// docs: https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps
private function getClient($redirect_uri = '') {
if (!empty($redirect_uri)) {
$this->redirect_uri = $redirect_uri;
}

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setScopes([
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/plus.profiles.read",
"https://www.googleapis.com/auth/youtube"
]);
// available scopes: https://developers.google.com/identity/protocols/googlescopes
$client->setRedirectUri($this->redirect_uri);
$client->setState(mt_rand());
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setIncludeGrantedScopes(true); // incremental auth

return $client;
}

public function setRefreshAccessToken($accessToken) {
// Set the access token
$this->client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($this->client->isAccessTokenExpired()) {
// update access token
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
}
}

// Insert a comment on video
// docs: https://developers.google.com/youtube/v3/docs/commentThreads/insert
public function commentVideo($videoId, $commentText) {
$this->client->addScope('https://www.googleapis.com/auth/youtube');
$this->client->addScope('https://www.googleapis.com/auth/youtube.force-ssl');

# Create a comment snippet with text.
$commentSnippet = new Google_Service_YouTube_CommentSnippet();
$commentSnippet->setTextOriginal($commentText);

# Create a top-level comment with snippet.
$topLevelComment = new Google_Service_YouTube_Comment();
$topLevelComment->setSnippet($commentSnippet);

# Create a comment thread snippet with channelId and top-level comment.
$commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
// $commentThreadSnippet->setChannelId($CHANNEL_ID);
$commentThreadSnippet->setVideoId($videoId); // Insert video comment
$commentThreadSnippet->setTopLevelComment($topLevelComment);

# Create a comment thread with snippet.
$commentThread = new Google_Service_YouTube_CommentThread();
$commentThread->setSnippet($commentThreadSnippet);

# Call the YouTube Data API's commentThreads.insert method to create a comment.
$response = $this->yt_service->commentThreads->insert('snippet', $commentThread);

// print_r($response);
return $response;
}

}
?>

这是我的端点脚本(我在此脚本上执行 ajax 调用)。

<php

// filename: comment.php

require_once("api.php");

// Insert a comment on youtube video.

if(empty($_POST['token'])) {
return die('parameter `token` is required.');
}
if(empty($_POST['videoId'])) {
return die('parameter `videoId` is required.');
}
if(empty($_POST['commentText'])) {
return die('parameter `commentText` is required.');
}

try {
// GoogleApi class instance
$api = new GoogleApi(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_CALLBACK, $_POST['token']);

$data = $api->commentVideo($_POST['videoId'], $_POST['commentText']);

// send response.
echo json_encode($result);
} catch(Exception $err) {
return die($err -> getMessage());
}

?>

最佳答案

如果您安慰 CommentThreads: insert 的文档

Authorization
This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope https://www.googleapis.com/auth/youtube.force-ssl

您会注意到,为了访问此方法,您需要包含此范围。

提示:如果您需要获得授权才能访问相关方法,则所有方法的文档都包含此授权部分。如果你总是检查它,那么你就不会再遇到这个问题。

关于php - Google Youtube API 插入评论总是返回错误响应 403 - "Insufficient Permission"- 域 "global",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50458183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com