我将使用YouTube API V3上传php中的YouTube API V3,如下所述:https://develovelers.google.com/youtube/v3/docs/videos/videos/videos/insert
我有这个功能
function uploadVideo($file, $title, $description, $tags, $categoryId, $privacy)
{
$token = getToken(); // Tested function to retrieve the correct AuthToken
$video->snippet['title'] = $title;
$video->snippet['description'] = $description;
$video->snippet['categoryId'] = $categoryId;
$video->snippet['tags'] = $tags; // array
$video->snippet['privacyStatus'] = $privacy;
$res = json_encode($video);
$parms = array(
'part' => 'snippet',
'file' => '@'.$_SERVER['DOCUMENT_ROOT'].'/complete/path/to/'.$file
'video' => $res
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/upload/youtube/v3/videos');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token['access_token']));
$return = json_decode(curl_exec($ch));
curl_close($ch);
return $return;
}
但它返回此
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => global
[reason] => badContent
[message] => Unsupported content with type: application/octet-stream
)
)
[code] => 400
[message] => Unsupported content with type: application/octet-stream
)
)
文件是mp4。
任何人都可以提供帮助?
更新版本:现在使用自定义上传URL并在上传过程中发送元数据。整个过程需要2个请求:
-
获取自定义上传位置
首先,提出邮政请求,要求上传URL:
"https://www.googleapis.com/upload/youtube/v3/videos"
您需要发送2个标题:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}" "Content-type": "application/json"
您需要发送3个参数:
"uploadType": "resumable" "part": "snippet, status" "key": {YOUR_API_KEY}
您需要在请求正文中发送视频的元数据:
{ "snippet": { "title": {VIDEO TITLE}, "description": {VIDEO DESCRIPTION}, "tags": [{TAGS LIST}], "categoryId": {YOUTUBE CATEGORY ID} }, "status": { "privacyStatus": {"public", "unlisted" OR "private"} } }
根据此请求,您应该在标题中获得一个"位置"字段的响应。
-
发布到自定义位置发送文件。
对于上传,您需要1个标头:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
并将文件作为数据/正文发送。
如果您阅读了他们的客户的工作方式。尽管我正在使用Python&urllib2而不是卷发。
另外,由于自定义上传位置,此版本可以重新启动,尽管我还没有需要。
不幸的是,我们还没有可用的YouTube API V3的特定示例,但我的一般建议是:
- 使用PHP客户库代替卷发。
- 将代码基于为驱动器API编写的示例。因为YouTube API V3与其他Google API共享一个常见的API基础架构,因此在不同服务中进行上传文件之类的示例应该非常相似。
- 查看需要在YouTube V3上传中设置的特定元数据的Python示例。
通常,您的卷曲代码有很多不正确的事情,我无法浏览修复它所需的所有步骤,因为我认为使用PHP客户端库是一个更好的选择。如果您确信您想使用卷曲
python脚本:
# categoryId is '1' for Film & Animation
# to fetch all categories: https://www.googleapis.com/youtube/v3/videoCategories?part=snippet®ionCode={2 chars region code}&key={app key}
meta = {'snippet': {'categoryId': '1',
'description': description,
'tags': ['any tag'],
'title': your_title},
'status': {'privacyStatus': 'private' if private else 'public'}}
param = {'key': {GOOGLE_API_KEY},
'part': 'snippet,status',
'uploadType': 'resumable'}
headers = {'Authorization': 'Bearer {}'.format(token),
'Content-type': 'application/json'}
#get location url
retries = 0
retries_count = 1
while retries <= retries_count:
requset = requests.request('POST', 'https://www.googleapis.com/upload/youtube/v3/videos',headers=headers,params=param,data=json.dumps(meta))
if requset.status_code in [500,503]:
retries += 1
break
if requset.status_code != 200:
#do something
location = requset.headers['location']
file_data = open(file_name, 'rb').read()
headers = {'Authorization': 'Bearer {}'.format(token)}
#upload your video
retries = 0
retries_count = 1
while retries <= retries_count:
requset = requests.request('POST', location,headers=headers,data=file_data)
if requset.status_code in [500,503]:
retries += 1
break
if requset.status_code != 200:
#do something
# get youtube id
cont = json.loads(requset.content)
youtube_id = cont['id']
我已经能够使用以下Shell脚本在YouTube上上传视频。
#!/bin/sh
# Upload the given video file to your YouTube channel.
cid_base_url="apps.googleusercontent.com"
client_id="<YOUR_CLIENT_ID>.$cid_base_url"
client_secret="<YOUR_CLIENT_SECRET>"
refresh_token="<YOUR_REFRESH_TOKEN>"
token_url="https://accounts.google.com/o/oauth2/token"
api_base_url="https://www.googleapis.com/upload/youtube/v3"
api_url="$api_base_url/videos?uploadType=resumable&part=snippet"
access_token=$(curl -H "Content-Type: application/x-www-form-urlencoded" -d refresh_token="$refresh_token" -d client_id="$client_id" -d client_secret="$client_secret" -d grant_type="refresh_token" $token_url|awk -F '"' '/access/{print $4}')
auth_header="Authorization: Bearer $access_token"
upload_url=$(curl -I -X POST -H "$auth_header" "$api_url"|awk -F ' |r' '/loc/{print $2}'); curl -v -X POST --data-binary "@$1" -H "$auth_header" "$upload_url"
请参阅此类似的问题,以获取如何获取自定义变量值。