YouTube:错误,需要登录?

问题描述 投票:1回答:1

我正在尝试使用googleapi在YouTube上播放一个事件,但它给我一个回复,并说错误说login required

我已经在我的开发者控制台中启用了YouTube API。

这是我的代码..

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

这是它返回的错误响应,

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

我提到了这个qazxsw poi和qazxsw poi,但它仍然没有任何帮助。

任何猜测?

node.js google-api youtube-api google-oauth
1个回答
0
投票

好吧,所以我想出了我的问题,这是我的最终代码。 (附一些评论提示)

Error: "message": "Login Required" when use Youtube Analytics API

问题是我使用"Login Required" error when trying to create live broadcast using YoutubeV3 API设置我的凭据,这是不需要的,因为我现在没有实现任何身份验证或身份验证(稍后我会在我的应用程序中附加)。使用它可能会初始化身份验证。流,所以我使用OAuth2代替。

我明确地传递了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 }); google-auth-library,我之前从我的应用程序验证,然后我存储它们。 (如果要实现身份验证,可能需要包含access_token)。

我在此之后做的另一个愚蠢的错误是,我插入了另一个“测试”应用程序的不同refresh_tokengoogle-auth-library。您不想这样做,请使用您用于身份验证的相同应用程序凭据。

希望这可以帮助。 ;)

© www.soinside.com 2019 - 2024. All rights reserved.