Apps Script doesn't seem to be able to access my YouTube, despite having permission

203 views
Skip to first unread message

Just Run It

unread,
Dec 2, 2022, 1:26:03 PM12/2/22
to Google Apps Script Community
I'm trying to use a pretty simple script to update my YouTube video's title automatically, but for some reason when it runs it's not updating. I know the script works and I know I've granted access to my account to run it, any ideas? Maybe there's a setting I'm missing?

Thanks

Just Run It

unread,
Dec 2, 2022, 1:26:50 PM12/2/22
to Google Apps Script Community
Here's the code: 

function updateTitle() {
  
  var videoID = 'iVckzzEMfUs'; //https://youtu.be/iVckzzEMfUs
  var part = 'snippet,statistics';
  var params = {'id': videoID};
  
  var response = YouTube.Videos.list(part, params);
  var video = response.items[0];
  var videoViewsCount = video.statistics.viewCount;
  var videoTitle = videoViewsCount + ' people have seen us get the boot';
  
  video.snippet.title = videoTitle;
  
  try{
    YouTube.Videos.update(video,part);
    
  }catch(e){
    
   }
}

Just Run It

unread,
Dec 2, 2022, 1:40:49 PM12/2/22
to Google Apps Script Community
GoogleScriptsScreenshot.PNG

cwl...@gmail.com

unread,
Dec 3, 2022, 12:15:03 PM12/3/22
to Google Apps Script Community
Dumb question, did you add the Youtube advanced services you need? In the editor, click on Services and make sure the Youtube services you need are added to your project.

Just Run It

unread,
Dec 7, 2022, 12:12:22 AM12/7/22
to Google Apps Script Community
Sorry for the delayed response, but yes the YouTube service I need has been added and still not updating. 

Thanks - Just Run It

cwl...@gmail.com

unread,
Dec 7, 2022, 4:24:08 PM12/7/22
to Google Apps Script Community
After much wring of hands and gnashing of teeth, I was able to get this code to finally work. You can probably refactor it to make it more streamlined, but I broke it down into two functions:
1 - the main function "updateVideo()"  
2 - helper function to get the video's view count "getCount()"

function getCount(vidId) {
  var part = 'snippet,statistics';
  var params = {'id': vidId};
  var response = YouTube.Videos.list(part, params);
  return response.items[0].statistics.viewCount
}


function updateVideo() {
  var vidId = "yourVideoID";
  var videoViewsCount = getCount(vidId); //call other function to retrieve count
  var originalTitle = "the permanent video title you want"
  var updatedTitle = `${originalTitle} -- has ${videoViewsCount} views `;
  var resource = {
      snippet: {
        title: updatedTitle,
        categoryId: '24'
      },
      id: vidId
    };
    YouTube.Videos.update(resource, 'id,snippet');
}

Just Run It

unread,
Dec 7, 2022, 11:31:00 PM12/7/22
to google-apps-sc...@googlegroups.com
Thanks for your work on this, when I try to run after updating with my video ID (see below), I receive the following error:

11:27:19 PM
Notice
Execution started
11:27:20 PM
Error
GoogleJsonResponseException: API call to youtube.videos.list failed with error: No filter selected. Expected one of: myRating, id, chart

Here's my code:

function getCount(vidId) {
  var part = 'snippet,statistics';
  var params = {'ID'vidId};
  var response = YouTube.Videos.list(partparams);
  return response.items[0].statistics.viewCount
}


function updateVideo() {
  var vidId = "VAdZUAvz4Rs";
  var videoViewsCount = getCount(vidId); //call other function to retrieve count
  var originalTitle = "the permanent video title you want"
  var updatedTitle = `${originalTitle} -- has ${videoViewsCount} views `;
  var resource = {
      snippet: {
        titleupdatedTitle,
        categoryId'24'
      },
      idvidId
    };
    YouTube.Videos.update(resource'id,snippet');
}

Thanks again! Btw I have YouTube Services added, let me know there are any Google Services I need to add.


--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/_1agVbqKSkw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4b94b329-e308-45ed-8631-b7d0dc327623n%40googlegroups.com.

cwl...@gmail.com

unread,
Dec 8, 2022, 7:13:36 AM12/8/22
to Google Apps Script Community
The Youtube service I am using is this one:
YouTube Data API v3

Make sure that is added to your project. 

cwl...@gmail.com

unread,
Dec 8, 2022, 7:17:28 AM12/8/22
to Google Apps Script Community
Also, just to make sure all authorizations are approved,   create,  then run/debug from script editor,  then delete the following:

function deleteMe() {
   console.log("This should prompt for any unapproved authorizations.")

Just Run It

unread,
Dec 8, 2022, 9:18:20 AM12/8/22
to google-apps-sc...@googlegroups.com
Okay added that, didn't work. Could you confirm from my last message that I put my video ID in the right location? It's highlighted.

cwl...@gmail.com

unread,
Dec 8, 2022, 11:12:13 AM12/8/22
to Google Apps Script Community
Yes, that is the correct place. 

Try creating a whole different script. Add the Youtube Data Api to the project, and paste in this code. 
https://developers.google.com/youtube/v3/code_samples/apps-script#update_video

Run the code and see if adds ' Description updated via Google Apps Script' to the latest video's descript (not title!) you have uploaded.
If that runs, then there is something wrong somewhere else, and I suggest modifying the working script you just created. That is how
I got the code I posted above, from this sample.

Just Run It

unread,
Dec 8, 2022, 10:11:52 PM12/8/22
to Google Apps Script Community
Got it working - thanks for your help on this!

cwl...@gmail.com

unread,
Dec 8, 2022, 10:59:52 PM12/8/22
to Google Apps Script Community
👍👍  You're welcome. Good luck!
Reply all
Reply to author
Forward
0 new messages