Last active
February 17, 2024 00:14
-
-
Save piyushchauhan2011/489dedff4329f1baea435918ab6dc869 to your computer and use it in GitHub Desktop.
Deno Google Sheets using google service accounts JWT authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require("jsonwebtoken"); | |
const credentials = require('./credentials.json'); | |
const privateKey = credentials["private_key"]; | |
const iss = credentials["client_email"]; | |
const scope = "https://www.googleapis.com/auth/drive.readonly"; | |
const token = jwt.sign( | |
{ | |
iss, | |
scope: , | |
aud: "https://oauth2.googleapis.com/token", | |
exp: 1594569178, | |
iat: 1594567978, | |
}, | |
privateKey, | |
{ | |
algorithm: "RS256", | |
header: { alg: "RS256", typ: "JWT" }, | |
} | |
); | |
console.log(token); | |
/* | |
curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<generated-jwt>' https://oauth2.googleapis.com/token | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PEM=$( cat pem.key ) | |
NOW=$( date +%s ) | |
IAT="${NOW}" | |
# expire 9 minutes in the future. 10 minutes is the max for github | |
EXP=$((${NOW} + 540)) | |
HEADER_RAW='{"alg":"RS256","typ":"JWT"}' | |
echo $HEADER_RAW | |
HEADER=$( echo -n "${HEADER_RAW}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' ) | |
PAYLOAD_RAW='{"iat":'"${IAT}"',"exp":'"${EXP}"',"iss":"","scope":"https://www.googleapis.com/auth/drive.readonly","aud":"https://oauth2.googleapis.com/token"}' | |
echo $PAYLOAD_RAW | |
PAYLOAD=$( echo -n "${PAYLOAD_RAW}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' ) | |
HEADER_PAYLOAD="${HEADER}"."${PAYLOAD}" | |
SIGNATURE=$( openssl dgst -sha256 -sign <(echo -n "${PEM}") <(echo -n "${HEADER_PAYLOAD}") | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' ) | |
JWT="${HEADER_PAYLOAD}"."${SIGNATURE}" | |
echo $JWT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main() { | |
const response = await fetch('https://sheets.googleapis.com/v4/spreadsheets/<sheet-id>/values/<sheet-name>!<sheet-range>', { | |
headers: { | |
'Authorization': 'Bearer <access-token>' | |
} | |
}); | |
const result = await response.json(); | |
console.log(result); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Links
# go to service account, create one if not present and generate api credentials