2011-09-09 9 views
2

다음 코드를 사용하면 direct upload을 통해 동영상 YouTube를 업로드하는 것이 좋습니다. 이미 액세스 토큰 (auth_key)이 있습니다. 나는 node.js/Youtube API/업로드

function upload(auth_key, devKey, callback){ 

    fs.readFile('test.mp4', function(err, movie){ 

     var boundary = randomString(); 

     var xml = 
      '<?xml version="1.0"?>' + 
      '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' + 
      '<media:group>' + 
      '<media:title type="plain">Bad Wedding Toast</media:title>' + 
      '<media:description type="plain">I gave a bad toast at my friends wedding.</media:description>' + 
      '<media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>' + 
      '<media:keywords>toast, wedding</media:keywords>' + 
      '</media:group>' + 
      '</entry>' 
     ; 

     var post_data = 
      '--' + boundary + '\n' + 
      'Content-Type: application/atom+xml; charset=UTF-8' + '\n' + 
      xml + '\n' + 
      '--' + boundary + '\n' + 
      'Content-Type: video/mp4' + '\n' + 
      'Content-Transfer-Encoding: binary' + '\n' + 
      movie + '\n' + 
      '--' + boundary + '--' + '\n' 
     ; 

     var options = { 
      host: 'uploads.gdata.youtube.com', 
      port: 443, 
      path: '/feeds/api/users/default/uploads', 
      method: 'POST', 
      headers: { 
       'Authorization:': 'GoogleLogin auth=' + auth_key, 
       'GData-Version': '2', 
       'X-GData-Key': 'key=' + devKey, 
       'Slug': 'test.mp4', 
       'Content-Type': 'multipart/related; boundary="' + boundary + '"', 
       'Content-Length': post_data.length, 
       'Connection': 'close' 
      } 
     } 

     var req = https.request(options, function(res) { 
      var response = ''; 
      res.on('data', function(chunk) { 
       response += chunk; 
      }); 
      res.on('end', function(){ 
       callback(response); 
      }); 
     }); 

     if(post_data){ 
      req.write(post_data); 
     } 

     req.end(); 

     req.on('error', function(e) { 
      console.error(e); 
     }); 

    }); 

} 

는 "잘못된 요청"로 실패 ... 내가 잘못 후 데이터를 전송하고있어 확실 해요?

답변

6

코드에 오류가 있습니다. html 요청의 구문에 "\ r \ n"이 (가) 없습니다. 또한 http 라이브러리와 포트 80을 사용해야합니다. 권한 부여 헤더가 잘못되었습니다. 끝에 ":"이 없어야합니다. 그리고 나는 당신이 데이터를 추가하고 내용의 길이를 계산하는 방식이 일을 엉망으로 만든다고 생각합니다.

var file_reader = fs.createReadStream(file_path, {encoding: 'binary'}); 
var file_contents = ''; 
file_reader.on('data', function(data) 
{ 
    file_contents += data; 
}); 
file_reader.on('end', function() 
{ 
    var xml = 
     '<?xml version="1.0"?>' + 
     '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' + 
     ' <media:group>' + 
     '  <media:title type="plain">' + title + '</media:title>' + 
     '  <media:description type="plain">' + description + '</media:description>' + 
     '  <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' + 
     '  <media:keywords>' + keywords + '</media:keywords>' + 
     ' </media:group>' + 
     '</entry>'; 

    var boundary = Math.random(); 
    var post_data = []; 
    var part = ''; 

    part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n"; 
    post_data.push(new Buffer(part, "utf8")); 

    part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n"; 
    post_data.push(new Buffer(part, 'ascii')); 
    post_data.push(new Buffer(file_contents, 'binary')); 
    post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii'); 

    var post_length = 0; 
    for(var i = 0; i < post_data.length; i++) 
    { 
     post_length += post_data[i].length; 
    } 

    var options = { 
     host: 'uploads.gdata.youtube.com', 
     port: 80, 
     path: '/feeds/api/users/default/uploads?alt=json', 
     method: 'POST', 
     headers: { 
      'Authorization': 'GoogleLogin auth=' + auth_key, 
      'GData-Version': '2', 
      'X-GData-Key': 'key=' + exports.developer_key, 
      'Slug': 'video.mp4', 
      'Content-Type': 'multipart/related; boundary="' + boundary + '"', 
      'Content-Length': post_length, 
      'Connection': 'close' 
     } 
    } 

    var req = http.request(options, function(res) 
    { 
     res.setEncoding('utf8'); 

     var response = ''; 
     res.on('data', function(chunk) 
     { 
      response += chunk; 
     }); 
     res.on('end', function() 
     { 
      console.log(response); 
      response = JSON.parse(response); 

      callback(response); 
     }); 
    }); 

    for (var i = 0; i < post_data.length; i++) 
    { 
     req.write(post_data[i]); 
    } 

    req.on('error', function(e) { 
     console.error(e); 
    }); 

    req.end(); 
}); 
:

나는 성공적으로 다음 코드로 유튜브에 동영상을 업로드 한