1

I'm trying to broadcast a event on YouTube using googleapi but it throwing me an response with an error saying login required.

I already have enabled the YouTube api in my developer console.

and here is my code..

const { google } = require('googleapis');
var OAuth2 = google.OAuth2;

const { OAuth2Client } = require('google-auth-library');

const auth = new OAuth2Client(
    'CLIENT_ID', //CLIENT_ID
    'CLIENT_SECRET', //MY_CLIENT_SECRET,
    'http://localhost:8000/'//YOUR_REDIRECT_URL
);

OAuth2Client.Credentials = {
    access_token: "access_token", // of client
    refresh_token: "refresh_token"  // of client

};

const youtube = google.youtube({
    version: 'v3',
    auth: OAuth2Client
});


broadcastParams = {
    "auth": OAuth2Client,
    "part": "snippet,status,contentDetails",
    "resource": {
        "snippet": {
            "title": "Tesing NodeJS 123",
            "scheduledStartTime": "2018-03-03T18:02:00.000Z",
            "scheduledEndTime": "2018-03-03T18:05:00.000Z",
        },
        "status": {
            "privacyStatus": "private",
        },
        "contentDetails": {
            "monitorStream": {
                "enableMonitorStream": true,
            }
        }
    }
};


youtube.liveBroadcasts.insert(broadcastParams, function (err, broadcast) {
    if (err) {
        return console.log('Error creating broadcast: ', err);
    }
       console.log('Broadcast = ' + JSON.stringify(broadcast));
});

here is the error response it returned ,

errors:
 [ { domain: 'global',
     reason: 'required',
     message: 'Login Required',
     locationType: 'header',
     location: 'Authorization' } ] }

i refered to this Error: "message": "Login Required" when use Youtube Analytics API and "Login Required" error when trying to create live broadcast using YoutubeV3 API but still it wasn't any help.

any guesses?

P.hunter
  • 1,345
  • 2
  • 21
  • 45

1 Answers1

1

Alright, so i figured out my problem, here is my final code. (with some commented tips)

const {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;   // initializing an oauth2 object
var oauth2Client = new OAuth2(
    'client_id', //CLIENT_ID
    'client_secret', // CLIENT_SECRET
);

oauth2Client.setCredentials({
    access_token: "access_token",   // you get these after after autherization of the client.
    refresh_token: "refresh_token"  // you only get it in the first time of the authentication as the authorization takes place first time only (for testing  purposes of your app you can you can disable it's permissions whenever you want from the permissions page)  

});


oauth2Client.refreshAccessToken((err, tokens) => {
// it is a good practice to refresh the tokens for testing purposes but you 
// would want to set some cookies for it and create some middle-ware to check 
// for expiration when running on stage.

});


const youtube = google.youtube({ // a "YouTube" object
    version: 'v3',
    "auth": oauth2Client,   
});


broadcastParams = {   // you can check parameters for in the docs here https://developers.google.com/youtube/v3/live/docs/ 

    "part": "snippet,status,contentDetails",
    "resource": {
        "snippet": {
            "title": "into the starry sky..",
            "scheduledStartTime": "2018-03-04T20:50:00.000Z", // format is important YYYY-MM-DDTHH:MM:SS.SSSZ (ex: 2014-02-22T18:00:00t.000Z where "Z" is the time zone)

             "scheduledEndTime": "2018-03-03T18:05:00.000Z",
        },
        "status": {
            "privacyStatus": "private", // public, unlisted or private
        },
        "contentDetails": {
            "monitorStream": {
                "enableMonitorStream": true,
            }
        }
    }
};

// TODO watch the auth parameters in broadcasting in the console testing
youtube.liveBroadcasts.insert(broadcastParams, function (err, broadcast) {
    if (err) {
        return console.log('Error creating broadcast: ', err);
    }

    console.log('Broadcast = ' + broadcast); // returns a broadcast object

});

the problem was that i was setting up my credentials using google-auth-library which wasn't needed as i wasn't implementing any authentication or authentication right now (which i'll attach later in my app.), using it maybe initializes the auth. flow, so i used OAuth2 instead.

i explicitly passed the access_token and refresh_token which i got earlier from authentication from my app then i stored them. (You may want to include the google-auth-library if you want to implement the authentication).

Another silly mistake i did after this was that, i inserted different client_id and client_secret of some another "test" app. You don't want to do that, put the same app's credentials you used for authentication.

hope this helps. ;)

P.hunter
  • 1,345
  • 2
  • 21
  • 45