File size: 1,616 Bytes
6f55a1e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
let path = require('path')
let fs = require('fs').promises
let { promisify } = require('util')
let { google } = require('googleapis')
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = path.join(__dirname, '..', 'token.json')
let
class GoogleAuth extends EventEmitter {
constructor() {
super()
}
async authorize(credentials) {
let token
const { client_secret, client_id } = credentials
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, `http://localhost:${port}`)
try {
token = JSON.parse(await fs.readFile(TOKEN_PATH))
} catch (e) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
this.emit('auth', authUrl)
let code = await promisify(this.once).bind(this)('token')
token = await oAuth2Client.getToken(code)
await fs.writeFile(TOKEN_PATH, JSON.stringify(token))
} finally {
await oAuth2Client.setCredentials(token)
}
}
token(code) {
this.emit('token', code)
}
}
class GoogleDrive extends GoogleAuth {
constructor() {
super()
this.path = '/drive/api'
}
async getFolderID(path) {
}
async infoFile(path) {
}
async folderList(path) {
}
async downloadFile(path) {
}
async uploadFile(path) {
}
}
module.exports = {
GoogleAuth,
GoogleDrive,
}
|