Skip to content

Commit

Permalink
Merge pull request #1 from stape-io/updating_google_auth
Browse files Browse the repository at this point in the history
Added Stape API, updated Google auth
  • Loading branch information
gvidoou authored Nov 4, 2024
2 parents b447d04 + 79058d5 commit 847892c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Google Sheets reader variable allows reading data from Google Sheet and add use
- [Server Google Tag Manager container](https://stape.io/blog/how-to-set-up-google-tag-manager-server-side-container)
- [Google Service Account](https://stape.io/blog/write-data-from-server-google-tag-manager-to-google-sheets#1-google-service-account)
- [Google Sheet Authentication](https://stape.io/blog/write-data-from-server-google-tag-manager-to-google-sheets#2-google-sheet-authentication)
- [Firebase account](https://stape.io/blog/write-data-from-server-google-tag-manager-to-google-sheets#3-firebase-account)


### How to use Google Sheet tag

Expand Down
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
homepage: "https://stape.io/"
versions:
- sha: 1a8976d84016fdceb0a7f5da4171bfe467569a23
changeNotes: Added Stape Api, updated Google auth.
- sha: 9d93bcf5feb9af77df07d563744dfac20e2267f0
changeNotes: Update to correct naming.
- sha: 01ac0b73447791063664b1ee819215650fe21d71
Expand Down
69 changes: 36 additions & 33 deletions template.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
const JSON = require('JSON');
const sendHttpRequest = require('sendHttpRequest');
const encodeUriComponent = require('encodeUriComponent');
const Firestore = require('Firestore');
const getGoogleAuth = require('getGoogleAuth');

let firebaseOptions = {};
if (data.firebaseProjectId) firebaseOptions.projectId = data.firebaseProjectId;
const spreadsheetId = data.url.replace('https://docs.google.com/spreadsheets/d/', '').split('/')[0];
const requestUrl = getUrl();
const auth = getGoogleAuth({
scopes: ['https://www.googleapis.com/auth/spreadsheets']
});

return Firestore.read(data.firebasePath, firebaseOptions)
.then((result) => {
return sendGetRequest(result.data.access_token, result.data.refresh_token);
}, () => {
return updateAccessToken(data.refreshToken);
});

function sendGetRequest(accessToken, refreshToken) {
const postUrl = getUrl();
return sendGetRequest();

return sendHttpRequest(postUrl, {headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + accessToken}, method: 'GET'}).then(successResult => {
function sendGetRequest() {
let params = {
headers: {'Content-Type': 'application/json', },
method: 'GET'
};
if (data.authFlow === 'own') {
params.authorization = auth;
}
return sendHttpRequest(requestUrl, params).then(successResult => {
let bodyParsed = JSON.parse(successResult.body);

if (successResult.statusCode >= 200 && successResult.statusCode < 400) {
Expand All @@ -32,34 +35,34 @@ function sendGetRequest(accessToken, refreshToken) {
}

return bodyParsed.values;
} else if (successResult.statusCode === 401) {
return updateAccessToken(refreshToken);
} else {
return '';
}
});
}

function updateAccessToken(refreshToken) {
const authUrl = 'https://oauth2.googleapis.com/token';
const authBody = 'refresh_token='+enc(refreshToken || data.refreshToken)+'&client_id='+enc(data.clientId)+'&client_secret='+enc(data.clientSecret)+'&grant_type=refresh_token';

return sendHttpRequest(authUrl, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}, method: 'POST'}, authBody).then((successResult) => {
if (successResult.statusCode >= 200 && successResult.statusCode < 400) {
let bodyParsed = JSON.parse(successResult.body);

return Firestore.write(data.firebasePath, bodyParsed, firebaseOptions)
.then((id) => {
return sendGetRequest(bodyParsed.access_token, bodyParsed.refresh_token);
}, () => { return ''; });
} else {
return '';
}
});
}

function getUrl() {
let spreadsheetId = data.url.replace('https://docs.google.com/spreadsheets/d/', '').split('/')[0];
if (data.authFlow === 'stape') {
const containerKey = data.containerKey.split(':');
const containerZone = containerKey[0];
const containerIdentifier = containerKey[1];
const containerApiKey = containerKey[2];
const containerDefaultDomainEnd = containerKey[3] || 'io';

return (
'https://' +
enc(containerIdentifier) +
'.' +
enc(containerZone) +
'.stape.' +
enc(containerDefaultDomainEnd) +
'/stape-api/' +
enc(containerApiKey) +
'/v1/spreadsheet/auth-proxy?spreadsheetId=' + spreadsheetId +
'&range=' + enc(data.type === 'cell' ? data.cell : data.range)
);
}

return 'https://content-sheets.googleapis.com/v4/spreadsheets/'+spreadsheetId+'/values/'+enc(data.type === 'cell' ? data.cell : data.range);
}
Expand Down
Loading

0 comments on commit 847892c

Please sign in to comment.