How to get video url with youtube api(v3)
Asked Answered
P

2

13

I'm working in node on a server and I'm trying to use the YouTube Api to get a video url. The particular url isn't important, this is more of just an exercise. As far as I can tell with the docs, the best way to find a particular video is if it is associated with a channel or user. IE,

  1. query the api with a username(GoogleDevelopers for example)
  2. get it's channel id
  3. query again to get that channels playlists
  4. search a particular playlist to get a playlist item
  5. given a playlistid, search with that to get items(each should represent a video)
  6. item ids have a videoId field, search with that
  7. get video info, but no url?

However, when I get to that point I feel like there should be a url to that video. The though process would be to send this url back to a front end to allow it to use some library to render the video.

QUESTION: Is there a better way to do this? If not, where am I missing the video URL,

Ploss answered 21/8, 2018 at 20:16 Comment(3)
Is this for the Data API yeah? Is anything else included in the return when you set part=snippet in the request?Ide
Yes, and yeah a couple of other fields(depending on which query I'm running) like kind, etag, nextPageToken etc.Ploss
sweet, you should also have the id object with the videoId hopefully, and my answer will make senseIde
I
28

In your return you should get a videoId:

"id": 
{
  "kind": "youtube#video",
  "videoId": "BsLvIF5c7NU"
}

edit: the return is actually just the id:

"id": "BsLvIF5c7NU"

All you need to do is just append that to the standard url no?:

var url = `https://www.youtube.com/watch?v=${result.id.videoId}`;

Does that make sense?

edit: You could also use part=contentDetails in which case the id is under:

result.items.id

Depending on what you use in the part param will change up the layout of what's returned.

Hope that helps you, dude.

Ide answered 21/8, 2018 at 20:33 Comment(0)
I
1

The answer by @dmccallum83 is correct.

If for some reason you can't (don't want to) concatenate strings -

const youtube = require("googleapis").youtube('v3');

async function getVideoUrl(videoId) {
  const res = await youtube.videos.list({
    part: "snippet,contentDetails",
    id: videoId,
  });

  const video = res.data.items[0];

  return video.snippet.resourceId.url;
}

const videoId = "YOUR_VIDEO_ID";

const videoUrl = await getVideoUrl(videoId);

Get the video id from the response of your upload request and use that to get the video details using the videos.list method which will return a list of videos, including the video url.

Immunize answered 23/5, 2023 at 10:59 Comment(1)
Neither the docs nor the type definitions show url as a property of resourceIdSupinate

© 2022 - 2024 — McMap. All rights reserved.