首页 > 解决方案 > 通过 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);
}

我试着做了几天我找不到路......

谢谢你的帮助!..................................................... ..................................................... ............

标签: youtube-apigoogle-oauthyoutube-data-apiphp-curl

解决方案


推荐阅读