YouTube API错误,Node.js var redirectUrl=credentials.installed.redirect_uris[0]; ^ TypeError:无法读取未定义的属性“0” at authorize(D:\Node.js\yt api\server.js:37:56) 在processClientSecrets(D:\Node.js\yt api\server.js:24:3) 在FSReqWrap.readFileAfterClose[完成时](fs.js:511:3) var express=需要(“express”); var server=express(); var fs=require('fs') var readline=require('readline'); var{google}=require('googleapis'); var OAuth2=google.auth.OAuth2; server.use(express.static('public')); //如果修改这些作用域,请删除以前保存的凭据 //位于~/.credentials/youtube-nodejs-quickstart.json 变量作用域=['https://www.googleapis.com/auth/youtube.readonly']; var TOKEN_DIR=(process.env.HOME | | process.env.HOMEPATH|| process.env.USERPROFILE)+'/.credentials/'; var TOKEN_PATH=TOKEN_DIR+'youtube nodejs quickstart.json'; //从本地文件加载客户端机密。 fs.readFile('client_secret.json',函数processClientSecrets(err,content){ 如果(错误){ log('加载客户端机密文件时出错:'+err); 返回; } //使用加载的凭据授权客户端,然后调用YouTube API。 授权(JSON.parse(content),getChannel); }); /** *使用给定的凭据创建OAuth2客户端,然后执行 *给定回调函数。 * *@param{Object}凭据授权客户端凭据。 *@param{function}回调使用授权客户端调用的回调。 */ 函数授权(凭据、回调){ var clientSecret=credentials.installed.client\u secret; var clientId=credentials.installed.client\u id; var redirectUrl=credentials.installed.redirect_uris[0]; var oauth2Client=新的OAuth2(clientId、clientSecret、redirectUrl); //检查我们以前是否存储过令牌。 fs.readFile(令牌\路径,函数(err,令牌){ 如果(错误){ getNewToken(oauth2Client,回调); }否则{ oauth2Client.credentials=JSON.parse(令牌); 回调(oauth2Client); } }); } /** *提示用户授权后获取并存储新令牌,然后 *使用授权的OAuth2客户端执行给定的回调。 * *@param{google.auth.OAuth2}OAuth2客户端为OAuth2客户端获取令牌。 *@param{getEventsCallback}回调使用授权的 *客户。 */ 函数getNewToken(oauth2Client,回调){ var authUrl=oauth2Client.generateAuthUrl({ 访问类型:“脱机”, 范围:范围 }); log('通过访问此url授权此应用:',authUrl); var rl=readline.createInterface({ 输入:process.stdin, 输出:process.stdout }); rl.question('在此处输入该页面的代码:',函数(代码){ rl.close(); oauth2Client.getToken(代码、函数(错误、令牌){ 如果(错误){ log('尝试检索访问令牌时出错',err); 返回; } oauth2Client.credentials=令牌; storeToken(token); 回调(oauth2Client); }); }); } /** *将令牌存储到磁盘,以便在以后的程序执行中使用。 * *@param{Object}标记要存储到磁盘的标记。 */ 函数storeToken(token){ 试一试{ fs.mkdirSync(TOKEN_DIR); }捕捉(错误){ if(err.code!=“EEXIST”){ 犯错误; } } fs.writeFile(TOKEN_PATH,JSON.stringify(TOKEN),(err)=>{ 如果(错误)抛出错误; console.log('存储到'+Token_PATH'的令牌); }); console.log('存储到'+Token_PATH'的令牌); } /** *列出最多10个文件的名称和ID。 * *@param{google.auth.OAuth2}对授权的OAuth2客户端进行身份验证。 */ 函数getChannel(auth){ var service=google.youtube('v3'); service.channels.list({ auth:auth, 部分:“代码段、内容详细信息、统计信息”, forUsername:“谷歌开发者” },函数(错误,响应){ 如果(错误){ log('API返回错误:'+err); 返回; } var通道=response.data.items; 如果(channels.length==0){ console.log('未找到通道'); }否则{ console.log('此频道的ID为%s。其标题为\'%s\',并且'+ “它有%s个视图。”, 通道[0]。id, 频道[0]。snippet.title, 通道[0]。统计信息。视图计数); } }); } var port=process.env.port | | 4001; 服务器。侦听(端口,()=>{ log(`listing on port${port}`) })

YouTube API错误,Node.js var redirectUrl=credentials.installed.redirect_uris[0]; ^ TypeError:无法读取未定义的属性“0” at authorize(D:\Node.js\yt api\server.js:37:56) 在processClientSecrets(D:\Node.js\yt api\server.js:24:3) 在FSReqWrap.readFileAfterClose[完成时](fs.js:511:3) var express=需要(“express”); var server=express(); var fs=require('fs') var readline=require('readline'); var{google}=require('googleapis'); var OAuth2=google.auth.OAuth2; server.use(express.static('public')); //如果修改这些作用域,请删除以前保存的凭据 //位于~/.credentials/youtube-nodejs-quickstart.json 变量作用域=['https://www.googleapis.com/auth/youtube.readonly']; var TOKEN_DIR=(process.env.HOME | | process.env.HOMEPATH|| process.env.USERPROFILE)+'/.credentials/'; var TOKEN_PATH=TOKEN_DIR+'youtube nodejs quickstart.json'; //从本地文件加载客户端机密。 fs.readFile('client_secret.json',函数processClientSecrets(err,content){ 如果(错误){ log('加载客户端机密文件时出错:'+err); 返回; } //使用加载的凭据授权客户端,然后调用YouTube API。 授权(JSON.parse(content),getChannel); }); /** *使用给定的凭据创建OAuth2客户端,然后执行 *给定回调函数。 * *@param{Object}凭据授权客户端凭据。 *@param{function}回调使用授权客户端调用的回调。 */ 函数授权(凭据、回调){ var clientSecret=credentials.installed.client\u secret; var clientId=credentials.installed.client\u id; var redirectUrl=credentials.installed.redirect_uris[0]; var oauth2Client=新的OAuth2(clientId、clientSecret、redirectUrl); //检查我们以前是否存储过令牌。 fs.readFile(令牌\路径,函数(err,令牌){ 如果(错误){ getNewToken(oauth2Client,回调); }否则{ oauth2Client.credentials=JSON.parse(令牌); 回调(oauth2Client); } }); } /** *提示用户授权后获取并存储新令牌,然后 *使用授权的OAuth2客户端执行给定的回调。 * *@param{google.auth.OAuth2}OAuth2客户端为OAuth2客户端获取令牌。 *@param{getEventsCallback}回调使用授权的 *客户。 */ 函数getNewToken(oauth2Client,回调){ var authUrl=oauth2Client.generateAuthUrl({ 访问类型:“脱机”, 范围:范围 }); log('通过访问此url授权此应用:',authUrl); var rl=readline.createInterface({ 输入:process.stdin, 输出:process.stdout }); rl.question('在此处输入该页面的代码:',函数(代码){ rl.close(); oauth2Client.getToken(代码、函数(错误、令牌){ 如果(错误){ log('尝试检索访问令牌时出错',err); 返回; } oauth2Client.credentials=令牌; storeToken(token); 回调(oauth2Client); }); }); } /** *将令牌存储到磁盘,以便在以后的程序执行中使用。 * *@param{Object}标记要存储到磁盘的标记。 */ 函数storeToken(token){ 试一试{ fs.mkdirSync(TOKEN_DIR); }捕捉(错误){ if(err.code!=“EEXIST”){ 犯错误; } } fs.writeFile(TOKEN_PATH,JSON.stringify(TOKEN),(err)=>{ 如果(错误)抛出错误; console.log('存储到'+Token_PATH'的令牌); }); console.log('存储到'+Token_PATH'的令牌); } /** *列出最多10个文件的名称和ID。 * *@param{google.auth.OAuth2}对授权的OAuth2客户端进行身份验证。 */ 函数getChannel(auth){ var service=google.youtube('v3'); service.channels.list({ auth:auth, 部分:“代码段、内容详细信息、统计信息”, forUsername:“谷歌开发者” },函数(错误,响应){ 如果(错误){ log('API返回错误:'+err); 返回; } var通道=response.data.items; 如果(channels.length==0){ console.log('未找到通道'); }否则{ console.log('此频道的ID为%s。其标题为\'%s\',并且'+ “它有%s个视图。”, 通道[0]。id, 频道[0]。snippet.title, 通道[0]。统计信息。视图计数); } }); } var port=process.env.port | | 4001; 服务器。侦听(端口,()=>{ log(`listing on port${port}`) }),node.js,oauth-2.0,youtube-data-api,Node.js,Oauth 2.0,Youtube Data Api,怎么把它修好?oAuth2有一些问题?您的客户端\u secret.json文件缺少重定向\u URI的密钥 请参见下面的示例文件: var express = require('express'); var server = express(); var fs = require('fs') var readline = require('readline'); var {google} = require('googleapis'); var OAut

怎么把它修好?oAuth2有一些问题?

您的
客户端\u secret.json
文件缺少
重定向\u URI的密钥

请参见下面的示例文件:

var express = require('express'); var server = express(); var fs = require('fs') var readline = require('readline'); var {google} = require('googleapis'); var OAuth2 = google.auth.OAuth2; server.use(express.static('public')); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/youtube-nodejs-quickstart.json var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']; var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; var TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json'; // Load client secrets from a local file. fs.readFile('client_secret.json', function processClientSecrets(err, content) { if (err) { console.log('Error loading client secret file: ' + err); return; } // Authorize a client with the loaded credentials, then call the YouTube API. authorize(JSON.parse(content), getChannel); }); /** * Create an OAuth2 client with the given credentials, and then execute the * given callback function. * * @param {Object} credentials The authorization client credentials. * @param {function} callback The callback to call with the authorized client. */ function authorize(credentials, callback) { var clientSecret = credentials.installed.client_secret; var clientId = credentials.installed.client_id; var redirectUrl = credentials.installed.redirect_uris[0]; var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl); // Check if we have previously stored a token. fs.readFile(TOKEN_PATH, function(err, token) { if (err) { getNewToken(oauth2Client, callback); } else { oauth2Client.credentials = JSON.parse(token); callback(oauth2Client); } }); } /** * Get and store new token after prompting for user authorization, and then * execute the given callback with the authorized OAuth2 client. * * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. * @param {getEventsCallback} callback The callback to call with the authorized * client. */ function getNewToken(oauth2Client, callback) { var authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES }); console.log('Authorize this app by visiting this url: ', authUrl); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Enter the code from that page here: ', function(code) { rl.close(); oauth2Client.getToken(code, function(err, token) { if (err) { console.log('Error while trying to retrieve access token', err); return; } oauth2Client.credentials = token; storeToken(token); callback(oauth2Client); }); }); } /** * Store token to disk be used in later program executions. * * @param {Object} token The token to store to disk. */ function storeToken(token) { try { fs.mkdirSync(TOKEN_DIR); } catch (err) { if (err.code != 'EEXIST') { throw err; } } fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { if (err) throw err; console.log('Token stored to ' + TOKEN_PATH); }); console.log('Token stored to ' + TOKEN_PATH); } /** * Lists the names and IDs of up to 10 files. * * @param {google.auth.OAuth2} auth An authorized OAuth2 client. */ function getChannel(auth) { var service = google.youtube('v3'); service.channels.list({ auth: auth, part: 'snippet,contentDetails,statistics', forUsername: 'GoogleDevelopers' }, function(err, response) { if (err) { console.log('The API returned an error: ' + err); return; } var channels = response.data.items; if (channels.length == 0) { console.log('No channel found.'); } else { console.log('This channel\'s ID is %s. Its title is \'%s\', and ' + 'it has %s views.', channels[0].id, channels[0].snippet.title, channels[0].statistics.viewCount); } }); } var port = process.env.port || 4001; server.listen(port, () => { console.log(`Listening on port ${port}`) }) var redirectUrl = credentials.installed.redirect_uris[0]; ^ TypeError: Cannot read property '0' of undefined at authorize (D:\Node.js\yt-api\server.js:37:56) at processClientSecrets (D:\Node.js\yt-api\server.js:24:3) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3) var express = require('express'); var server = express(); var fs = require('fs') var readline = require('readline'); var {google} = require('googleapis'); var OAuth2 = google.auth.OAuth2; server.use(express.static('public')); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/youtube-nodejs-quickstart.json var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']; var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; var TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json'; // Load client secrets from a local file. fs.readFile('client_secret.json', function processClientSecrets(err, content) { if (err) { console.log('Error loading client secret file: ' + err); return; } // Authorize a client with the loaded credentials, then call the YouTube API. authorize(JSON.parse(content), getChannel); }); /** * Create an OAuth2 client with the given credentials, and then execute the * given callback function. * * @param {Object} credentials The authorization client credentials. * @param {function} callback The callback to call with the authorized client. */ function authorize(credentials, callback) { var clientSecret = credentials.installed.client_secret; var clientId = credentials.installed.client_id; var redirectUrl = credentials.installed.redirect_uris[0]; var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl); // Check if we have previously stored a token. fs.readFile(TOKEN_PATH, function(err, token) { if (err) { getNewToken(oauth2Client, callback); } else { oauth2Client.credentials = JSON.parse(token); callback(oauth2Client); } }); } /** * Get and store new token after prompting for user authorization, and then * execute the given callback with the authorized OAuth2 client. * * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. * @param {getEventsCallback} callback The callback to call with the authorized * client. */ function getNewToken(oauth2Client, callback) { var authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES }); console.log('Authorize this app by visiting this url: ', authUrl); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Enter the code from that page here: ', function(code) { rl.close(); oauth2Client.getToken(code, function(err, token) { if (err) { console.log('Error while trying to retrieve access token', err); return; } oauth2Client.credentials = token; storeToken(token); callback(oauth2Client); }); }); } /** * Store token to disk be used in later program executions. * * @param {Object} token The token to store to disk. */ function storeToken(token) { try { fs.mkdirSync(TOKEN_DIR); } catch (err) { if (err.code != 'EEXIST') { throw err; } } fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { if (err) throw err; console.log('Token stored to ' + TOKEN_PATH); }); console.log('Token stored to ' + TOKEN_PATH); } /** * Lists the names and IDs of up to 10 files. * * @param {google.auth.OAuth2} auth An authorized OAuth2 client. */ function getChannel(auth) { var service = google.youtube('v3'); service.channels.list({ auth: auth, part: 'snippet,contentDetails,statistics', forUsername: 'GoogleDevelopers' }, function(err, response) { if (err) { console.log('The API returned an error: ' + err); return; } var channels = response.data.items; if (channels.length == 0) { console.log('No channel found.'); } else { console.log('This channel\'s ID is %s. Its title is \'%s\', and ' + 'it has %s views.', channels[0].id, channels[0].snippet.title, channels[0].statistics.viewCount); } }); } var port = process.env.port || 4001; server.listen(port, () => { console.log(`Listening on port ${port}`) })
{
  "installed": {
    "client_id": "YOUR_CLIENT_ID_HERE",
    "client_secret": "YOUR_CLIENT_SECRET_HERE",
    "redirect_uris": ["http://localhost:8080/oauth2callback"],
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token"
  }
}