Youtube API 在尝试调用插入时 chrome 扩展出现 "No filter selected" 错误
Youtube API getting "No filter selected" error on chrome extension when trying to call insert
我正在尝试通过 chrome 扩展程序调用 youtube API 在我的频道上制作播放列表,但我一直收到此错误代码;
domain: "youtube.parameter"
location: "parameters."
locationType: "other"
message: "No filter selected. Expected one of: channelId, id, mine"
reason: "missingRequiredParameter"
这是我的清单文件:
"action": {
"default_title": "multi monitor for youtube"
},
"oauth2": {
"client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scopes": [
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtubepartner",
"https://www.googleapis.com/auth/youtube"
]
},
"permissions": [
"identity"
],
"host_permissions": ["https://www.googleapis.com/"],
"background": {
"service_worker": "background.js"
},
"manifest_version": 3
这是我的背景文件
chrome.action.onClicked.addListener(function(){
chrome.identity.getAuthToken({ interactive: true }, function (token) {
console.log(token);
let fetch_options = {
//method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
"part": [
"snippet,status"
],
"resource": {
"snippet": {
"title": "Sample playlist created via API",
"channelId": "UC0E5pDp_c2riLV4UhEgynKA",
"description": "This is a sample playlist description.",
"tags": [
"sample playlist",
"API call"
],
"defaultLanguage": "en"
},
"status": {
"privacyStatus": "private"
},
}
};
fetch(
'https://www.googleapis.com/youtube/v3/playlists',
fetch_options
)
.then((response) => response.json()) // Transform the data into json
.then(function (data) {
console.log(data);//contains the response of the created event
});
});
});
我尝试使用 this 问题的第二个答案,但必须进行一些更改才能调用 youtube API 而不是日历。
因为我遇到了错误,所以我尝试将频道 ID 添加到代码段中,但它仍然不起作用。
我对创建 chrome 扩展很陌生,所以如果这里有明显的错误,请原谅我。
我终于明白了!
所以我必须改变的第一件事是我必须将我的 API 键和 part=snippet 添加到获取 URL 中。其次,我查看了 fetch 方法,发现您需要使用 body 指定消息的主体:JSON.stringify()。
所以这是我最终得到的background.js
chrome.identity.getAuthToken({ interactive: true }, function (token) {
console.log(token);
let fetchString = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=<API key>'
let post =
{
"part": [
"kind,snippet,channelTitle,status"
],
"kind": "youtube#playlistItem",
"snippet":{
"title": "Sample playlist created via API",
"channelID": "<channel ID>",
"description": "This is a sample playlist description.",
"tags": [
"sample playlist",
"API call"
],
"defaultLanguage": "en",
"status": {
"privacyStatus": "private"
}
}
}
let fetchOptions = {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(post),
}
fetch(fetchString,fetchOptions)
.then((response) => response.json()) // Transform the data into json
.then(function (data) {
console.log(data);//contains the response of the created event
});
});
我正在尝试通过 chrome 扩展程序调用 youtube API 在我的频道上制作播放列表,但我一直收到此错误代码;
domain: "youtube.parameter"
location: "parameters."
locationType: "other"
message: "No filter selected. Expected one of: channelId, id, mine"
reason: "missingRequiredParameter"
这是我的清单文件:
"action": {
"default_title": "multi monitor for youtube"
},
"oauth2": {
"client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scopes": [
"https://www.googleapis.com/auth/youtube.force-ssl",
"https://www.googleapis.com/auth/youtubepartner",
"https://www.googleapis.com/auth/youtube"
]
},
"permissions": [
"identity"
],
"host_permissions": ["https://www.googleapis.com/"],
"background": {
"service_worker": "background.js"
},
"manifest_version": 3
这是我的背景文件
chrome.action.onClicked.addListener(function(){
chrome.identity.getAuthToken({ interactive: true }, function (token) {
console.log(token);
let fetch_options = {
//method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
"part": [
"snippet,status"
],
"resource": {
"snippet": {
"title": "Sample playlist created via API",
"channelId": "UC0E5pDp_c2riLV4UhEgynKA",
"description": "This is a sample playlist description.",
"tags": [
"sample playlist",
"API call"
],
"defaultLanguage": "en"
},
"status": {
"privacyStatus": "private"
},
}
};
fetch(
'https://www.googleapis.com/youtube/v3/playlists',
fetch_options
)
.then((response) => response.json()) // Transform the data into json
.then(function (data) {
console.log(data);//contains the response of the created event
});
});
});
我尝试使用 this 问题的第二个答案,但必须进行一些更改才能调用 youtube API 而不是日历。 因为我遇到了错误,所以我尝试将频道 ID 添加到代码段中,但它仍然不起作用。 我对创建 chrome 扩展很陌生,所以如果这里有明显的错误,请原谅我。
我终于明白了! 所以我必须改变的第一件事是我必须将我的 API 键和 part=snippet 添加到获取 URL 中。其次,我查看了 fetch 方法,发现您需要使用 body 指定消息的主体:JSON.stringify()。 所以这是我最终得到的background.js
chrome.identity.getAuthToken({ interactive: true }, function (token) {
console.log(token);
let fetchString = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=<API key>'
let post =
{
"part": [
"kind,snippet,channelTitle,status"
],
"kind": "youtube#playlistItem",
"snippet":{
"title": "Sample playlist created via API",
"channelID": "<channel ID>",
"description": "This is a sample playlist description.",
"tags": [
"sample playlist",
"API call"
],
"defaultLanguage": "en",
"status": {
"privacyStatus": "private"
}
}
}
let fetchOptions = {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(post),
}
fetch(fetchString,fetchOptions)
.then((response) => response.json()) // Transform the data into json
.then(function (data) {
console.log(data);//contains the response of the created event
});
});