Created
October 20, 2019 17:21
-
-
Save CesarBalzer/4ace5dc72d0624b054557df0da3c4cc8 to your computer and use it in GitHub Desktop.
Google Drive Upload Image and Make Public and Get Link with NodeJS
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
/* | |
// credentials.json | |
{ | |
"installed": { | |
"client_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com", | |
"project_id": "XXXXXXXXXXXXXXXXXXXXXXXX", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_secret": "XXXXXXXXXXXXXXXXXXXXXXXX", | |
"redirect_uris": [ | |
"urn:ietf:wg:oauth:2.0:oob", | |
"http://localhost" | |
] | |
} | |
} | |
// token.json (https://developers.google.com/oauthplayground/) | |
{ | |
"access_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | |
"refresh_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | |
"scope": "https://www.googleapis.com/auth/drive", | |
"token_type": "Bearer", | |
"expiry_date": 1571480084721 | |
} | |
*/ | |
const { google } = require("googleapis") | |
const fs = require("fs") | |
const sha1 = require("sha1") | |
class GoogleDrive { | |
constructor(google, fs){ | |
this.google = google | |
this.fs = fs | |
this.credentialsFile = "credentials.json" | |
this.tokenFile = "token.json" | |
} | |
authenticate(){ | |
return new Promise((resolve,reject) => { | |
try{ | |
this.credentials = JSON.parse(this.fs.readFileSync(this.credentialsFile)).installed | |
const { client_id, client_secret, redirect_uris } = this.credentials | |
this.oauth2 = new this.google.auth.OAuth2(client_id, client_secret, redirect_uris[0]) | |
this.token = JSON.parse(this.fs.readFileSync(this.tokenFile)) | |
this.oauth2.setCredentials(this.token) | |
this.oauth2.on("tokens", (tokens) => { | |
let token = this.token | |
if (tokens.refresh_token) { | |
token.refresh_token = tokens.refresh_token | |
} | |
token.access_token = tokens.access_token | |
this.fs.writeFileSync(this.tokenFile, JSON.stringify(token)) | |
}); | |
resolve(this.oauth2) | |
}catch(e){ | |
reject(e) | |
} | |
}) | |
} | |
upload(image){ | |
return new Promise(async (resolve,reject) => { | |
try{ | |
const auth = await this.authenticate() | |
const drive = google.drive({ version: "v3", auth }) | |
drive.files.create({ | |
resource: { | |
"name": sha1(Math.random() * Math.random()) + ".png" | |
}, | |
media: { | |
mimeType: "image/png", | |
body: image | |
}, | |
fields: "id" | |
}, (error, uploadResponse) => { | |
if(!error){ | |
drive.permissions.create({ | |
resource: { | |
"type": "anyone", | |
"role": "reader" | |
}, | |
fileId: uploadResponse.data.id, | |
fields: "id" | |
}, (error, permissionResponse) => { | |
if(!error){ | |
resolve({ | |
url: "https://drive.google.com/uc?export=view&id=" + uploadResponse.data.id | |
}) | |
}else{ | |
reject(error) | |
} | |
}) | |
}else{ | |
reject(error) | |
} | |
}) | |
}catch(e){ | |
reject(e) | |
} | |
}) | |
} | |
} | |
const GoogleDrive = new GoogleDrive(google, fs) | |
GoogleDrive.upload(fs.createReadStream("exemplo.png")).then((data) => { | |
console.log(data) | |
/* | |
{ | |
url: "https://drive.google.com/uc?export=view&id=XXXXXXXXXXXXXXXXXX" Image Link | |
} | |
*/ | |
}).catch((error) => { | |
console.log("Error:", error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment