Category: nodejs

1 Posts

nodejs对接YouTube Live api
Auth by oAuth2 require: client_id, client_secret const env = { google: { clientId: '', clientSecret: '', redirect: '', // redirect url. api server地址,带上特定path,完成认证后会回调此地址 }, }; const { google } = npm.googleapis const OAuth2 = google.auth.OAuth2; const oAuth2Client = new google.auth.OAuth2( env.google.clientId, env.google.clientSecret, env.google.redirect ); const SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']; const authUrl = oAuth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES }); // send the authUrl by email or any other return authUrl; Auth Callback 拿到authUrl后,引导用户访问authUrl,然后跳转到回调地址redirect,得到code。比如回调地址: /auth/google 在认证完成后,会发起请求: /auth/google?code= 在请求的处理方法中,根据code获取token。 const env = { google: { clientId: '', clientSecret: '', redirect: '', // redirect url. api server地址,带上特定path,完成认证后会回调此地址 }, }; const { google } = npm.googleapis const oAuth2Client = new google.auth.OAuth2( env.google.clientId, env.google.clientSecret, env.google.redirect ) const tokenRes = await oAuth2Client.getToken(CODE); // CODE is from query oAuth2Client.setCredentials(tokenRes.tokens); 至此得到oAuth2Client 创建Broadcast const { google } = npm.googleapis; const authClient = await getOAuth2Client(); // 前面得到的 oAuth2Client const youtube = google.youtube('v3'); google.options({auth: authClient}) const res = await youtube.liveBroadcasts.insert({ part: [ "id,snippet,contentDetails,status" ], resource: { contentDetails: { enableEmbed: true, monitorStream: { enableMonitorStream: false // 如果这里不设置,默认为true,则必须先test才能live,否则一直提示 invalidTransition } }, snippet: { scheduledStartTime: 'start_time', title, description, }, status: {…