-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathService_Account.gs
More file actions
66 lines (58 loc) · 1.86 KB
/
Service_Account.gs
File metadata and controls
66 lines (58 loc) · 1.86 KB
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
/*
* To test Service Account run function testSA()
*
*/
const KEY = {
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "-----BEGIN PRIVATE KEY-----\n.....\n-----END PRIVATE KEY-----\n",
"client_email": "",
"client_id": ""
}
/**
* Authorizes and makes a request to the Google Drive API.
*/
function testSA() {
var service = getService();
if (service.hasAccess()) {
var url = 'https://storage.googleapis.com/storage/v1/b/'+BUCKET_NAME;
var response = UrlFetchApp.fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log('You have access to the Bucket : ' + result.name);
} else {
Logger.log(service.getLastError());
}
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
getService().reset();
}
/**
* Configures the service.
*/
function getService() {
return OAuth2.createService('STORAGE' )
// Set the endpoint URL.
.setTokenUrl('https://oauth2.googleapis.com/token')
// Set the private key and issuer.
.setPrivateKey(KEY.private_key)
.setIssuer(KEY.client_email)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
// .setSubject(USER_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope(['https://www.googleapis.com/auth/devstorage.read_write']);
}