-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"axios": "^0.24.0", | ||
"googleapis": "^92.0.0", | ||
"qs": "^6.10.1" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { google } = require('googleapis'); | ||
|
||
(async () => { | ||
const client = new google.auth.OAuth2( | ||
'507952577459-nkp348iglr2gs7fepn6ek4sn65qugllv.apps.googleusercontent.com', | ||
'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', | ||
'http://localhost' | ||
); | ||
|
||
const url = client.generateAuthUrl({ | ||
access_type: 'offline', | ||
scope: [ | ||
'https://www.googleapis.com/auth/youtube' | ||
] | ||
}); | ||
|
||
console.log(url) | ||
|
||
const { tokens } = await client.getToken('4/0AX4XfWggMxidf57Tf1J24pEi3M-YOLQUjP7TqpwO3mgfenA0yKJK-QCv8Yvk-Z7LhIDfAg'); | ||
|
||
console.log(tokens) | ||
})(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const axios = require('axios'); | ||
const qs = require('qs'); | ||
|
||
const CLIENT_ID = '507952577459-nkp348iglr2gs7fepn6ek4sn65qugllv.apps.googleusercontent.com'; | ||
const CLIENT_SECRET = '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'; | ||
|
||
const API_KEY = 'AIzaSyDNrXAUzEEknjgL2f7buQ-mM85mvUD3uhk'; | ||
const REFRESH_TOKEN = '1//01Yk8MtpmtPG5CgYIARAAGAESNwF-L9Ir6XHVLUIAGxaCHY85u3yC1tZQgmd271nhO6y4ggJASKvvwnidwBAYtay3_b1_9DHQsoc'; | ||
|
||
(async () => { | ||
// step 1 - get access token | ||
let res1 = await axios | ||
.post( | ||
'https://oauth2.googleapis.com/token', | ||
qs.stringify({ | ||
refresh_token: REFRESH_TOKEN, | ||
grant_type: 'refresh_token' | ||
}), | ||
{ | ||
auth: { | ||
username: CLIENT_ID, | ||
password: CLIENT_SECRET | ||
} | ||
} | ||
); | ||
|
||
const token = res1.data.access_token; | ||
|
||
// step 2 - get video information | ||
let res2 = await axios | ||
.get( | ||
'https://www.googleapis.com/youtube/v3/videos', | ||
{ | ||
params: { | ||
id: 'uuuTKf3y3VI', | ||
part: 'snippet,statistics', | ||
key: API_KEY | ||
} | ||
}, | ||
); | ||
|
||
let data = res2.data.items[0]; | ||
|
||
let { categoryId, title, description, tags } = data.snippet; | ||
let { viewCount } = data.statistics; | ||
|
||
// step 3 - update video | ||
await axios | ||
.put( | ||
'https://www.googleapis.com/youtube/v3/videos?part=snippet', | ||
{ | ||
id: 'uuuTKf3y3VI', | ||
snippet: { | ||
categoryId, | ||
title: `This video has ${viewCount} views`, | ||
description, | ||
tags, | ||
} | ||
}, | ||
{ | ||
headers: { | ||
authorization: 'Bearer ' + token | ||
} | ||
} | ||
); | ||
})(); |