问题描述
我有一个Web应用程序,通过它我可以创建播放列表,将视频添加到播放列表,删除视频等到我的YouTube频道。我按照示例YouTube API quickstart for nodejs进行了操作,在过去将近2周的时间里,所有内容都在本地运行,但是似乎无处不在,每次尝试调用API创建API时,我都会受到Error: Unauthorized
的打击例如播放列表。
{
message: 'Unauthorized',status: undefined,stackHighlighted: 'Error: Unauthorized\n' +
' at Gaxios._request (c:\\apps\\node\\myapp\\node_modules\\gaxios\\build\\src\\<mark>gaxios.js:89:23</mark>)\n' +
' at processticksAndRejections (internal/process/<mark>task_queues.js:93:5</mark>)\n' +
' at async oauth2client.requestAsync (c:\\apps\\node\\myapp\\node_modules\\google-auth-library\\build\\src\\auth\\<mark>oauth2client.js:343:18</mark>)\n' +
' at async createPlaylist (file:///c:/apps/node/myapp/controllers/youtube.<mark>controller.js:76:22</mark>)'
}
第76行是我打电话给youtube.playlists.insert
的地方。
唯一改变的是我将应用程序移动到了实时服务器,因此现在我的本地副本和服务器上的副本都无法正常工作。我的代码基本上如下所示。
var fs = require('fs');
var readline = require('readline');
var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;
// If modifying these scopes,delete your prevIoUsly saved credentials
// at ~/.credentials/youtube-nodejs-quickstart.json
var ScopES = ['https://www.googleapis.com/auth/youtube'];
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,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);
});
}
/**
* 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'
},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);
}
});
}
我不明白为什么会这样。任何见识将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)