【问题标题】:PHP - 404 Video Not Found (Videos: PUT) YouTube APIPHP - 404 视频未找到(视频:PUT)YouTube API
【发布时间】:2017-09-13 01:40:59
【问题描述】:

我正在创建一个小型网络应用程序,允许人们在 YouTube 之外更新他们的视频。我目前正在为自己测试,我遇到了

{ "error": { "errors": [ { "domain": "youtube.video", "reason": "videoNotFound", "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct.", "locationType": "other", "location": "body.id" } ], "code": 404, "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct." } }

我确定的事情:

  • 我有正确的授权码
  • 我拥有此授权码所指的正确渠道。
  • 我的视频 ID 正确。

我在 PHP 中使用 cURL 发送PUT 请求,如下所示:

$curl = curl_init($url . "https://www.googleapis.com/youtube/v3/videos?part=snippet&access_token=".$token)

$data = array(
'kind' => 'youtube#video',
'id' => 'theCorrectIdIsHere',
'snippet' => array(
    "title" => "Test title done through php",
    "categoryId" => "1"
),
);`

那么当我使用以下命令执行此操作时:

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: 
application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));`

有人问了一个类似的问题:Update title and description using YouTube v3 API?,但是,答案涉及他下载和替换视频,这需要额外的配额并且根本不应该这样做

注意在这里完成的 API 测试一切正常:https://developers.google.com/youtube/v3/docs/videos/update

【问题讨论】:

    标签: php api curl youtube youtube-api


    【解决方案1】:

    使用json_encode 设置请求数据并使用Authorization 标头设置访问令牌:

    <?php
    
    $curl = curl_init();
    
    $access_token = "YOUR_ACCESS_TOKEN";
    
    $data = array(
    'kind' => 'youtube#video',
    'id' => 'YOUR_VIDEO_ID',
    'snippet' => array(
        "title" => "Test title done through php",
        "categoryId" => "1"
    )
    );
    curl_setopt($curl,CURLOPT_URL, "https://www.googleapis.com/youtube/v3/videos?part=snippet");
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $access_token));
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    
    $result = curl_exec($curl);
    
    curl_close($curl);
    
    var_dump($result);
    ?>
    

    【讨论】:

    • 太棒了!你知道为什么它不适合我吗?
    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2015-11-19
    • 2017-11-21
    • 2017-04-26
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多