node.js / YouTube API / 上传

发布于 2024-12-03 14:31:50 字数 2536 浏览 2 评论 0原文

使用以下代码,我尝试通过直接上传。我已经拥有访问令牌(auth_key)。我很确定我发送的帖子数据不正确...

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

    });

}

是否因“无效请求”而失败?

Using the following code I'm tring to upload a video youtube via direct upload. I already have the access token (auth_key). I'm pretty sure I'm sending the post data incorrectly...

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

    });

}

Is failing with "Invalid Request"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-12-10 14:31:50

您的代码中有一些错误,html 请求的语法缺少“\r\n”。您还应该使用 http 库和端口 80。您的 Authorization 标头无效,它不应在末尾包含“:”。我认为你添加数据和计算内容长度的方式也会把事情搞砸。

我已使用以下代码成功将视频上传到 youtube:

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();
});

You have some errors in your code, the syntax of the html request is missing "\r\n". You should also use the http library and port 80. Your Authorization header is invalid, it should not contain an ":" at the end. And I think the way your are adding the data and calculating the content length also messes things up.

I've successfully uploaded videos to youtube with the following code:

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();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文