Webhook Youtube api

Hi all,

I m trying to use the YouTube api from Appsheet to retrieve a friendlier url. I looked at this page https://developers.google.com/youtube/v3/docs/videos/list and they suggest to redirect questions on stackoverflow. See my post:
https://stackoverflow.com/questions/64940134/how-to-capture-the-list-id-automatically-generated-by-youtube

I assume this can be done via a Webhook. I haven’t made any webhook yet so I am looking into some guidance on how I could try to implement the solution given on stackoverflow.

Has anyone done something similar using the YouTube api?

Thank you!

1 Like

Hey @teambelair, I don’t know if this is possible - from a brief glance it looks like it might be, but I’m not sure how to exactly get it done.

Could explain why you’re needing to do this? Is it just that you don’t want a horrible looking URL displayed in your app?

Have you checked out HYPERLINK()?

You could also put the URL inside an action and use a button instead.

2 Likes

Hyperlink could be an alternative but users would have to enter their email to get this url (I have a public app account so users do not authenticate). I would prefer that users easily access that url so they can copy and share it. To do so, I created a virtual field and display it on a form (I hide the cancel and save buttons).

The main problem is that the url is cut of after 200 characters which means that the copied url is invalid. Even if it wasn’t cut off, having a short url would be much nicer.

I also have another need of a webhook to retrieve the YouTube video duration so I am mostly interested to learn on how to integrate Appsheet with the YouTube REST api.

Thanks!

1 Like

@Steve do you recall someone integrating Appsheet with the Youtube api?

1 Like

teambelair:

Hyperlink could be an alternative but users would have to enter their email to get this url

Why would this require the user to enter an email??

I hear you about being ineterested about the integration, but to solve your problem directly you could try the following.

1 Like

From what I see here (about getting details from the YouTube API)

Looks like you’d need to have some intermediary to reformat things from the YouTube API result to what the AppSheet API needs.

Perhaps Zapier…

2 Likes

Hi Alex,

I wrote a small cloud function for you.

Give this a shot -
https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids=1EPwH8cXB-M,QohH89Eu5iM,OMKiiSMrL5w,QohH89Eu5iM,RG3ik4VacAA,QohH89Eu5iM,e5lPjg0GhFM,QohH89Eu5iM,QsL7H856rGs,QohH89Eu5iM,lO5qDs2iEpE,QohH89Eu5iM,ZYaWAHJZXm0

7 Likes

Bellave_Jayaram:

Hi Alex,> > I wrote a small cloud function for you.

I love this community!!!
3X_d_5_d51363a862e7ab883241c312ac5d7f271579cdd3.gif

3X_4_3_43b0545cb5e8988b7ec7695514535f2832c327ab.gif

4 Likes

@Bellave_Jayaram this is really cool. Thank you. How do I leverage that in Appsheet to display the result of this cloudfunction? Basically, how do I execute this url on the back end of Appsheet and share the result to my users?

1 Like

@MultiTech_Visions this was exactly my words

Also, I quickly looked into Zapier but didn’t see a built-in workflow to do that. The trigger would be the creation of a record X on a Google sheet, calling YouTube api to get that shorter url, then updating X. I’ll do more research on that. Thanks!

1 Like

There’s some manual work to do this. Create an action in the detail view of type ‘External: Go to a website’ and put this as the URL to go to:

CONCATENATE(“[https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids=“,[Related](https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids=“,[Related) IDs])

Where
[Related IDs] is the comma separated list.

When you click the action, it should give you the short URL. Add a real column to the sheet and copy and paste it into the relevant cell.

Automatic population to a cell is doable with an app script bound to your sheet that uses URLFetchapp.fetch()

3 Likes

Thank you

When you click the action, it should give you the short URL. Add a real column to the sheet and copy and paste it into the relevant cell.

This is not going to be convenient since users are generating dynamically the id list.

Automatic population to a cell is doable with an app script bound to your sheet that uses URLFetchapp.fetch()

Yes! I am going to read this to see if I can do that: How to Integrate Google Apps Script & Trigger with an AppSheet App

Also would you mind sharing how and what tool you used to create that cloud function?

1 Like

I initially tried to do it using app script but it doesn’t support the HEAD method so I wrote a Python function and deployed it as a Google Cloud Function. If you need the code fragment that does the job, let me know and I will post it.

3 Likes

That would be very appreciated. Thank you

I use google app script to retrieve the video duration. Works very well!
Found the script here: https://stackoverflow.com/questions/51561919/google-sheet-formula-to-convert-youtubes-api-iso-8601-duration-format

1 Like

Here is the google app script you will need. Note that you will have to pass the list of IDs in the JSON payload from the AppSheet webhook and also replace the Logger.log() line with the code to write the value to the correct cell.

function doPost  (e) {
  
  var json = e.postData.contents;
  var body = JSON.parse(json);
  var params = {"method" : "GET"};
  var response = UrlFetchApp.fetch('https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids='+body.ids, params);    
  var code = response.getResponseCode()
  if (code === 200) {
     var location = response.getContentText('utf-8')
     Logger.log(location)
  }
}
2 Likes

you will have to pass the list of IDs in the JSON payload from the AppSheet webhook and also replace the Logger.log() line with the code to write the value to the correct cell.

Sorry I don’t know what that means.

Can’t I use this function the same way i use this one to retrieve the YouTube video duration?

function getYoutubeTime(videoId){
      var url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + videoId;
      url = url + "&key=MYKEY";
      var videoListResponse = UrlFetchApp.fetch(url);
      var json = JSON.parse(videoListResponse.getContentText());
      return json["items"][0]["contentDetails"]["duration"];
    }

I created the google app script and called it on right google sheet column:
=getYoutubeTime(P3) where P3 is the column containing the list of video ids.

Can I do something like that?
function doPost (videoIds) {

  var json = e.postData.contents;
  var body = JSON.parse(json);
  var params = {"method" : "GET"};
  var response = UrlFetchApp.fetch('https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids='+videoIds, params);    
  var code = response.getResponseCode()
  if (code === 200) {
     var location = response.getContentText('utf-8')
     return location
  }
}
1 Like

Yes, you could certainly do that.

2 Likes

Works great. Thank you!

function getYoutubeListId3(e){
  var params = {"method" : "GET"};
  var response = UrlFetchApp.fetch('https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids='+e, params);  
  var code = response.getResponseCode()
  if (code === 200) {
     var location = response.getContentText('utf-8')
     return location
  }
}

Can you share with me the python script? I am going to check that tutorial: https://rominirani.com/google-cloud-functions-tutorial-writing-our-first-google-cloud-function-a62de60b5c90

1 Like

I don’t know why I can’t access the cloud function page… Need to reach out to google support.

1 Like

@teambelair
You can’t reach the google function codepage as it’s owned by ABLE3 domain’s cloud account.

Bellave_Jayaram:

There’s some manual work to do this. Create an action in the detail view of type ‘External: Go to a website’ and put this as the URL to go to:> > CONCATENATE(“[https://us-central1-able3api.cloudfunctions.net/youtubeshorturl?url=https://www.youtube.com/watch_videos?video_ids=“,Related IDs])

1 Like