-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathAirtable.gs
118 lines (101 loc) · 3.73 KB
/
Airtable.gs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* This sample demonstrates how to configure the library for the Airtable API.
* Instructions on how to generate OAuth keys is available here:
* https://airtable.com/developers/web/api/oauth-reference
*/
var CLIENT_ID = 'YOUR_CLIENT_ID';
var CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
var AIRTABLE_BASE_ID = 'YOUR_BASE_ID';
var AIRTABLE_TABLE_NAME = 'TABLE_NAME';
var AIRTABLE_VIEW_NAME = 'VIEW';
var AIRTABLE_FIELDS = ["Name", "IP Address", "Serial No", "Product Name", "Location", "Category Type", "Acquisition Date", "Notes"];
var URL_FIELDS = AIRTABLE_FIELDS.map(function (name) {
return 'fields=' + encodeURIComponent(name);
}).join('&');
/**
* Authorizes and makes a request to the Airtable API.
*/
function pullRecords() {
var service = getService_();
if (service.hasAccess()) {
var url = 'https://api.airtable.com/v0/' + AIRTABLE_BASE_ID + '/' + encodeURIComponent(AIRTABLE_TABLE_NAME) + '?view=' + encodeURIComponent(AIRTABLE_VIEW_NAME) + '&' + URL_FIELDS;
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
var service = getService_();
service.reset();
PropertiesService.getUserProperties().deleteProperty('code_challenge');
PropertiesService.getUserProperties().deleteProperty('code_verifier');
}
/**
* Configures the service.
* Three required parameters are not specified because
* the library creates the authorization URL with them
* automatically: `redirect_url`, `response_type`, and
* `state`.
*/
function getService_() {
var userProps = PropertiesService.getUserProperties();
return OAuth2.createService('Airtable')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('https://airtable.com/oauth2/v1/authorize')
//.setTokenUrl('https://airtable.com/oauth2/v1/token?code_verifier=' + userProps.getProperty('code_verifier'))
.setTokenUrl('https://airtable.com/oauth2/v1/token')
// Rest of the configuration
.setTokenPayloadHandler(function (tokenPayload) {
tokenPayload.code_verifier = PropertiesService.getUserProperties().getProperty('code_verifier');
return tokenPayload;
})
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback_')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(userProps)
// Set scope (required)
.setScope('data.records:read')
// Generate code verifier parameter
.generateCodeVerifier()
// Add parameters in the authorization url
.setParam('response_type', 'code')
.setParam('code_challenge_method', 'S256')
.setParam('code_challenge', userProps.getProperty('code_challenge'))
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET),
'Content-Type': 'application/x-www-form-urlencoded'
});;
}
/**
* Handles the OAuth callback.
*/
function authCallback_(request) {
var service = getService_();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
/**
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}