'Youtube API getting "No filter selected" error on chrome extension when trying to call insert
I'm trying to call the youtube API to make a playlist on my channel from a chrome extension and I keep getting this error code returned back to me;
domain: "youtube.parameter"
location: "parameters."
locationType: "other"
message: "No filter selected. Expected one of: channelId, id, mine"
reason: "missingRequiredParameter"
this is my manifest file:
"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
and this is my background file
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
});
});
});
I tried to use the second answer from this question but there are some changes that had to be made in order to call the youtube API instead of the calendar. Because I was getting the error I tried adding the channel ID to the snippet and it still doesn't work. I'm very new to creating chrome extensions so please forgive me if there is an obvious mistake here.
Solution 1:[1]
I finally figured it out! so the first thing that I had to change was I had to add my API key and part=snippet to the fetch URL. Second I looked into the fetch method and I found out that you needed to specify the body of the message with body : JSON.stringify(). so this is the background.js that I ended up with
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
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Peter |