-
-
Save RuurdBijlsma/bad525ce011a0d08eeece6e02a7d1c5b to your computer and use it in GitHub Desktop.
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
import {google} from "googleapis"; | |
import fs from 'fs' | |
const oauth2Client = new google.auth.OAuth2( | |
"0asdf90asdf0asd08f-id", | |
"0asdf90asdf0asd08f-secret", | |
"http://localhost:38901", | |
); | |
function getUrl() { | |
return oauth2Client.generateAuthUrl({ | |
// 'online' (default) or 'offline' (gets refresh_token) | |
access_type: 'offline', | |
// If you only need one scope you can pass it as a string | |
scope: [ | |
"https://www.googleapis.com/auth/youtube.upload", | |
"https://www.googleapis.com/auth/youtube.readonly" | |
], | |
}); | |
} | |
async function getTokens() { | |
const code = '0asdf90asdf0asd08f-code'; | |
oauth2Client.on('tokens', (tokens) => { | |
if (tokens.refresh_token) { | |
// store the refresh_token in my database! | |
console.log("refresh", tokens.refresh_token); | |
} | |
console.log("access", tokens.access_token); | |
}); | |
const {tokens} = await oauth2Client.getToken(code) | |
oauth2Client.setCredentials(tokens); | |
console.log("Afterwards", tokens); | |
} | |
function authorize() { | |
let refresh = '0asdf90asdf0asd08f-token'; | |
oauth2Client.setCredentials({ | |
refresh_token: refresh, | |
}) | |
} | |
async function upload() { | |
authorize(); | |
let filePath = 'C:/Users/Ruurd/Videos/soep.mp4'; | |
let fileSize = fs.statSync(filePath).size; | |
let service = google.youtube('v3') | |
let title = 'test1' + Math.random(); | |
console.log("Uploading", title); | |
let res = await service.videos.insert({ | |
auth: oauth2Client, | |
part: 'snippet,status', | |
resource: { | |
snippet: { | |
title, | |
description: 'test' | |
}, | |
status: { | |
privacyStatus: "unlisted" | |
}, | |
}, | |
media: { | |
mimeType: 'video/mp4', | |
body: fs.createReadStream(filePath) | |
}, | |
}, { | |
onUploadProgress: function (e) { | |
console.log(e.bytesRead / fileSize); | |
} | |
}); | |
console.log(res); | |
} | |
// console.log(getUrl()); | |
// await getTokens(); | |
await upload(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment