forked from openzipkin/zipkin-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
43 lines (40 loc) · 1.46 KB
/
upload.js
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
const fs = require('fs');
const GoogleAuth = require('google-auth-library');
const fetch = require('node-fetch');
const appId = 'jdpmaacocdhbmkppghmgnjmfikeeldfe';
const data = fs.createReadStream('dist/zipkin-chrome-extension.zip');
const clientId = '1077732158179-kdth2msv27g08oair1pvo3apu3ievrsm.apps.googleusercontent.com';
const clientSecret = fs.readFileSync('client-secret.txt', 'utf-8').trim();
const refreshToken = fs.readFileSync('refresh-token.txt', 'utf-8').trim();
const auth = new GoogleAuth();
const OAuth2 = auth.OAuth2;
const oauth2Client = new OAuth2(clientId, clientSecret, 'urn:ietf:wg:oauth:2.0:oob');
const webstoreScope = 'https://www.googleapis.com/auth/chromewebstore';
const scopes = [webstoreScope];
oauth2Client.setCredentials({
refresh_token: refreshToken
});
oauth2Client.refreshAccessToken((err, tokens) => {
if (err) {
console.error('oh no!');
console.error(err);
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
console.error('get a new token here', url);
} else {
// https://developer.chrome.com/webstore/using_webstore_api#uploadexisitng
fetch('https://www.googleapis.com/upload/chromewebstore/v1.1/items/' + appId, {
method: 'PUT',
body: data,
headers: {
'Authorization': 'Bearer ' + tokens.access_token,
'x-goog-api-version': '2'
}
})
.then(res => res.text())
.then(console.log)
.catch(console.error);
}
});