YouTube:错误,需要登录



我正在尝试使用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' } ] }

我提到了此错误:"消息":使用 Youtube 分析 API 时"需要登录",尝试使用 YoutubeV3 API 创建直播时出现"需要登录"错误,但仍然没有任何帮助。

有什么猜测吗?

好的,所以我解决了我的问题,这是我的最终代码。(有一些评论提示(

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设置我的凭据,因为我现在没有实现任何身份验证或身份验证(稍后我将在我的应用程序中附加(,使用它可能会初始化身份验证流,所以我使用了 OAuth2 代替。

我明确传递了之前从我的应用程序中进行身份验证时获得的access_tokenrefresh_token,然后我存储了它们。(如果要实现身份验证,则可能需要包含google-auth-library(。

在此之后我犯的另一个愚蠢的错误是,我插入了不同的client_idclient_secret另一个"测试"应用程序。您不想这样做,请放置用于身份验证的同一应用的凭据。

希望这有帮助。 ;)

最新更新