2017-04-17 53 views
1

我正在创建一个小型网络应用,允许用户在YouTube之外更新他们的视频。目前我测试了我自己,我跑入PHP - 404未找到视频(视频:PUT)YouTube API

{ "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。

我使用卷曲发送PUT请求PHP如下:

$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

回答

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

惊人!你知道为什么它不适合我吗? – ConorReidd