1
votes

I use NodeJS, I want to insert in my script a function to insert a video to a specific playlist.

I saw a lot on internet, and I know Youtube api requires OAuth to work, and so, some HTML page / callback to get the Token.

Did I miss something or is it impossible to handle this token in script without any user interaction ?

Is there a simple example of how doing this ? I tried this using youtube-api npm :

var express = require('express');
var router = express.Router(),
   Youtube = require("youtube-api"),
   fs = require('fs'),

router.get('/insert', function(req, res, next) {
    Youtube.authenticate({
        type: "oauth"
      , token: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZ9F0sVpqEXh"
    });

    var req = Youtube.videos.insert({
        "resource": {
            // Video title and description
            "snippet": {
                "title": "Test",
                "description": "Test video upload via YouTube API"
            },
            "status": {
                "privacyStatus": "private"
            }
        }, 
        "part": "snippet,status,id", 
        "media": {
            "body": fs.createReadStream('./test.mp4')
        }
    }, function (err, data) {
        console.log(err);

        // insert to playlist
        Youtube.playlistItems.insert({
            "resource": {
                "snippet": {
                    "playlistId": "xxxxxxxxxxxxxxxxxxxgvjIu",
                    "resourceId": {
                      "kind": "youtube#video",
                      "videoId": data.id // ???
                    }
                },
                "status": {
                    "privacyStatus": "private"
                }
            }

        }, function (err, data) {
            if (err) {
                return console.log(err); 
            }
        });         
    });

    res.json();
});

I got error 401 :

message: 'Login Required'

EDIT 1

Good video to start : https://www.youtube.com/watch?v=hfWe1gPCnzc

I got a token now, but don't know how to get no limit no this... Now I changed :

Youtube.authenticate({
    type: "oauth"
  , token: "token_from_google"
});

And I got the error :

message: 'The request cannot be completed because you have exceeded your quota.' } ],
code: 403, message: 'The request cannot be completed because you have exceeded your quota.

I can't understand it ... ?

1

1 Answers

0
votes

You have reach the allotted limit (quota). See the Core API errors for reference.

The YouTube Data API uses a quota to ensure that developers use the service as intended and do not create applications that unfairly reduce service quality or limit access for others. All API requests, including invalid requests, incur at least a one-point quota cost. You can find the quota available to your application in the Developers Console.

Projects that enable the YouTube Data API have a default quota allocation of 1 million units per day, an amount sufficient for the overwhelming majority of our API users. Default quota, which is subject to change, helps us optimize quota allocations and scale our infrastructure in a way that is more meaningful to our API users. You can see your quota usage on the Usage tab for the API in the Google Developer's Console.

Note: If you reach the quota limit, you can request additional quota on the Quotas tab in the Developer's Console.