【发布时间】:2020-12-06 04:47:37
【问题描述】:
我有一个网络应用程序,我希望通过该应用程序能够创建播放列表、将视频添加到播放列表、删除视频等到我的 youtube 频道。我创建了一个服务帐户并下载了服务帐户凭据密钥文件,并在 Google Developer Console 中设置了我的 OAuth 2.0 客户端 ID。
为了验证我的应用,我按照README.md 中的说明操作google-api-nodejs-client 此处https://github.com/googleapis/google-api-nodejs-client - 在服务帐户凭据
这是我的控制器文件...
我应该注意到该项目使用 ES 模块,因此"type": "module" 设置在package.json 中。这就是为什么你会注意到例如我将 __dirname 作为实用程序导入的原因,因为 ES 模块不支持常规的 __dirname。
import googleapi from "googleapis";
const { google } = googleapi;
import Auth from "@google-cloud/local-auth";
const { authenticate } = Auth;
import path from "path";
import __dirname from "../utils/dirname.js";
async function initialize() {
try {
const auth = await authenticate({
keyfilePath: path.join(__dirname, "../service_account_credentials.json"),
scopes: ["https://www.googleapis.com/auth/youtube"],
});
console.log("Auth details");
console.log(auth);
google.options({ auth });
} catch (e) {
console.log(e);
}
}
initialize();
const oauth2Client = new google.auth.OAuth2(
"YOUR_CLIENT_ID",
"YOUR_CLIENT_SECRET",
"http://localhost:5000/oauth2callback"
);
// initialize the Youtube API library
const youtube = google.youtube({ version: "v3", auth: oauth2Client });
class YoutubeController {
static async createPlaylist(req, res) {
const { name } = req.body;
const playlist = await youtube.playlists.insert({
part: "snippet,status",
resource: {
snippet: {
title: name,
description: `${name} videos.`,
},
status: {
privacyStatus: "private",
},
},
});
res.json(playlist);
}
}
initialize 函数是引发错误的函数,我不太明白。我想正因为如此,当我向在类中调用方法createPlaylist 的路由发出POST 请求时,我会返回No access, refresh token or API key is set.
我一直在阅读文档,试图了解一切是如何流动的,但我有点卡住了。
在这里提出了类似的问题 - TypeError: Cannot read property 'redirect_uris' of undefined,但没有答案,建议的工作流程不适用于我的情况,因此非常感谢您对此提供的帮助。
【问题讨论】:
标签: node.js google-api google-oauth youtube-data-api google-api-nodejs-client