ConorReidd
ConorReidd

Reputation: 276

PHP - 404 Video Not Found (Videos: PUT) YouTube API

I'm creating a small web app allowing people to update their video outside of YouTube. I'm currently testing for myself and i'm running into the

{ "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." } }

Things that I know for certain:

I'm sending a PUT request using cURL in PHP as follows:

$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"
),
);`

So how come when I execute this using:

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));`

A similar question has been asked: Update title and description using YouTube v3 API?, however, the answer involved him downloading and replacing the video which costs extra quota and simply shouldn't have to be done

Note Everything works fine on the API test done here: https://developers.google.com/youtube/v3/docs/videos/update

Upvotes: 1

Views: 546

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45493

Use json_encode to set the request data & using Authorization header to set access token :

<?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);
?>

Upvotes: 1

Related Questions