Skip to content

Instantly share code, notes, and snippets.

@kuboon
Created December 7, 2020 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuboon/074188de6935d67c3e6c8d24360ceea6 to your computer and use it in GitHub Desktop.
Save kuboon/074188de6935d67c3e6c8d24360ceea6 to your computer and use it in GitHub Desktop.
GCP api call sample for https://scriptable.app/
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: light-gray; icon-glyph: magic;
async function accessToken(){
const env = {
CLIENT_ID: "000000000.apps.googleusercontent.com",
CLIENT_SECRET: "xxxxxxx",
REFRESH_TOKEN: "1//xxxxxxxx"
}
const project = "myproject"
const zone = "asia-northeast1-b"
const instance = "xxx"
let url = "https://www.googleapis.com/oauth2/v4/token"
let r = new Request(url)
r.method = "POST"
r.addParameterToMultipart("client_id", env.CLIENT_ID)
r.addParameterToMultipart("client_secret", env.CLIENT_SECRET)
r.addParameterToMultipart("refresh_token", env.REFRESH_TOKEN)
r.addParameterToMultipart("grant_type", "refresh_token")
let body = await r.loadJSON()
return body.access_token
}
async function run(action){
const url = `https://compute.googleapis.com/compute/v1/projects/${project}/zones/${zone}/instances/${instance}/${action}`
const token = await accessToken()
let r = new Request(url)
r.method = "POST"
r.headers = {"Authorization": "Bearer " + token}
const res = await r.loadJSON()
return res
}
const action = args.plainTexts[0]
const res = await run(action)
Script.setShortcutOutput(res.selfLink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment