Created
March 9, 2022 21:36
-
-
Save toshvelaga/007aecdd41bfbaa2ea137822e1f4d13f to your computer and use it in GitHub Desktop.
get youtube view count using Node JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'), | |
router = express.Router(), | |
{ default: axios } = require('axios') | |
require('dotenv').config() | |
router.post('/api/youtube/view-count', async (req, res) => { | |
// https://developers.google.com/youtube/v3/docs/videos/list?apix=true&apix_params=%7B%22part%22%3A%5B%22statistics%2C%20status%22%5D%2C%22id%22%3A%5B%22_IrFoihwTUc%22%5D%7D#parameters | |
const youtubeBroadcastId = req.body.youtubeBroadcastId | |
const youtubeAccessToken = req.body.youtubeAccessToken | |
let viewCount = await axios | |
.get( | |
`https://youtube.googleapis.com/youtube/v3/videos?part=statistics%2C%20status&id=${youtubeBroadcastId}&key=${process.env.GOOGLE_API_KEY}`, | |
{ | |
headers: { | |
Authorization: `Bearer ${youtubeAccessToken}`, | |
'Content-Type': 'application/json', | |
}, | |
} | |
) | |
.then((res) => { | |
console.log(res.data.items[0].statistics.viewCount) | |
return res.data.items[0].statistics.viewCount | |
}) | |
.catch((err) => { | |
console.log(err) | |
}) | |
return res.status(201).send({ views: viewCount }) | |
}) | |
module.exports = router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment