2012-11-07 121 views
9

我会上传使用与PHP卷曲的Youtube API V3的视频,如下所述:https://developers.google.com/youtube/v3/docs/videos/insert在Youtube上传的视频使用curl和API V3

我有这个功能

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。

任何人都可以帮忙吗?

回答

0

不幸的是,我们没有YouTube的API V3上传从PHP提供的一个具体的例子还没有,但我一般建议是:

  • 使用PHP client library而不是卷曲。
  • 将您的代码基于为Drive API编写的this example。由于YouTube API v3与其他Google API共享一个通用的API基础结构,所以在不同服务中,上传文件等应用程序的示例应该非常相似。
  • 查看需要在YouTube v3上传中设置的特定元数据的Python example

一般来说,你的cURL代码有很多不正确的事情,我不能通过修复它的所有步骤,因为我认为使用PHP客户端库是一个好得多选项。如果你确信你想使用cURL,那么我会推迟给别人提供特定的指导。

+0

谢谢!我试图让东西更轻,但我会尝试谷歌官方库 – genna

+0

在Google_YoutubeService.php中我找不到调用我需要的插入方法的函数。所有方法都列出(频道,播放列表,视频..) 我必须等待较新版本的图书馆? – genna

+0

由于缺少我需要的方法,我决定使用Zend_Gdata_YouTube和API的第二个版本 – genna

16

更新版本:现在使用自定义上传网址并通过上传过程发送元数据。整个过程需要2个请求:

  1. 获取自定义上传位置

    首先,做一个POST请求上传网址:

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

    从这个要求,你应该得到与在头一个“位置”字段的响应。

  2. 发送到自定义位置发送文件。

    有关上载你需要1头:

    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}" 
    

    和发送的文件作为数据/身体。

如果你读通过他们的客户是如何工作的,你会看到他们建议重试,如果你正在返回的代码500,502,503,504或错误很明显,你会希望有重试和之间的等待周期最大重试次数。它每次都在我的系统中运行,尽管我正在使用python & urllib2而不是cURL。

此外,由于自定义上传位置此版本是上传可恢复能力,虽然我还没有需要。

+1

I使用POST上传请求设置参数也有问题,您是否仍然使用2个请求上传和更新元数据? – hsafarya

+0

我现在不在了。我改变了我的方法,使其更稳定,我将编辑我对新方法的回答。 –

+0

这个新方法的任何更新? – Mikepote

0

python脚本:

# categoryId is '1' for Film & Animation 
# to fetch all categories: https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode={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'] 
0

我已经能够使用下面的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" 

请参考this关于如何获取自定义变量值的类似问题。

相关问题