Skip to content

Commit 8c605ba

Browse files
author
Eric Koleda
authored
Add example for the Tink API. (#227)
1 parent 630db1f commit 8c605ba

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

samples/Tink.gs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* This sample demonstrates how to configure the library for the Tink API.
3+
* Instructions on how to generate OAuth credentuals is available here:
4+
* https://docs.tink.com/resources/getting-started/connect-tink-link
5+
*/
6+
7+
var CLIENT_ID = '...';
8+
var CLIENT_SECRET = '...';
9+
10+
/**
11+
* Authorizes and makes a request to the Tink API.
12+
*/
13+
function run() {
14+
var service = getService();
15+
if (service.hasAccess()) {
16+
// Make a request to retrieve user information.
17+
var url = 'https://api.tink.com/api/v1/user';
18+
var response = UrlFetchApp.fetch(url, {
19+
headers: {
20+
Authorization: 'Bearer ' + service.getAccessToken()
21+
},
22+
});
23+
var result = JSON.parse(response.getContentText());
24+
Logger.log(JSON.stringify(result, null, 2));
25+
} else {
26+
var authorizationUrl = service.getAuthorizationUrl();
27+
Logger.log('Open the following URL and re-run the script: %s',
28+
authorizationUrl);
29+
}
30+
}
31+
32+
/**
33+
* Reset the authorization state, so that it can be re-tested.
34+
*/
35+
function reset() {
36+
getService().reset();
37+
}
38+
39+
/**
40+
* Configures the service.
41+
*/
42+
function getService() {
43+
var service = OAuth2.createService('Tink')
44+
// Set the endpoint URLs.
45+
.setAuthorizationBaseUrl('https://link.tink.com/1.0/authorize/')
46+
.setTokenUrl('https://api.tink.com/api/v1/oauth/token')
47+
48+
// Set the client ID and secret.
49+
.setClientId(CLIENT_ID)
50+
.setClientSecret(CLIENT_SECRET)
51+
52+
// Set the name of the callback function that should be invoked to
53+
// complete the OAuth flow.
54+
.setCallbackFunction('authCallback')
55+
56+
// Set the property store where authorized tokens should be persisted.
57+
.setPropertyStore(PropertiesService.getUserProperties())
58+
59+
// Set the scopes to request from the user.
60+
// https://docs.tink.com/api/#introduction-authentication-authentication-scopes
61+
.setScope('identity:read user:read')
62+
63+
// Use testing providers (instead of real data).
64+
// https://docs.tink.com/resources/aggregation/use-test-providers
65+
.setParam('test', 'true');
66+
67+
// Determine if the user data still exists.
68+
if (service.hasAccess()) {
69+
// Make a request to retrieve identity information.
70+
var url = 'https://api.tink.com/api/v1/identities';
71+
var response = UrlFetchApp.fetch(url, {
72+
headers: {
73+
Authorization: 'Bearer ' + service.getAccessToken()
74+
},
75+
muteHttpExceptions: true
76+
});
77+
if (response.getResponseCode() == 401) {
78+
// The user data has been removed after 24 hours. Reset the service.
79+
// https://docs.tink.com/glossary#permanent-users
80+
service.reset();
81+
}
82+
}
83+
84+
return service;
85+
};
86+
87+
/**
88+
* Handles the OAuth callback.
89+
*/
90+
function authCallback(request) {
91+
var service = getService();
92+
var authorized = service.handleCallback(request);
93+
if (authorized) {
94+
return HtmlService.createHtmlOutput('Success!');
95+
} else {
96+
return HtmlService.createHtmlOutput('Denied.');
97+
}
98+
}
99+
100+
/**
101+
* Logs the redict URI to register in the Dropbox application settings.
102+
*/
103+
function logRedirectUri() {
104+
Logger.log(OAuth2.getRedirectUri());
105+
}

0 commit comments

Comments
 (0)