Last updated
var private = "-----BEGIN PRIVATE KEY-----${NEWLINE}key goes here${NEWLINE}-----END PRIVATE KEY-----"
var email = "user@account.iam.gserviceaccount.com"
var url = "https://www.googleapis.com/oauth2/v4/token"
var scope = "https://www.googleapis.com/auth/cloud-platform"
var now = ${UNIX_UTC}
var expiry = (${now} + 3600)
var header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"
var payload = "{\"iss\":\"${email}\",\"scope\":\"${scope}\",\"aud\":\"${url}\",\"iat\":\"${now}\",\"exp\":\"${expiry}\"}"
generate_jwt key ${private} ${header} ${payload} as JWT
# Make HTTP request according to https://developers.google.com/identity/protocols/OAuth2ServiceAccount
set http_header "Content-Type: application/x-www-form-urlencoded"
set http_body data "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${JWT}"
buffer token = HTTP POST "${url}"
if (${HTTP_STATUS_CODE} != 200) {
print Got HTTP status ${HTTP_STATUS_CODE}, expected a status of 200
print The server response was:
json format {token}
print {token}
terminate
}
var access_token = $JSON{token}.[access_token]
print Access token: ${access_token}