Skip to content

Instantly share code, notes, and snippets.

@wallabyway
Last active April 28, 2023 01:58
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 wallabyway/69021259070e18b207e879b0cdc3917b to your computer and use it in GitHub Desktop.
Save wallabyway/69021259070e18b207e879b0cdc3917b to your computer and use it in GitHub Desktop.
minimal access-token generator (with AWS-lambda & Claudia.js)
{
"lambda": {
"role": "web-api-test-executor",
"name": "web-api-test",
"region": "us-east-1"
},
"api": {
"id": "rtvnonbl58",
"module": "web"
}
}
/**
* Copy/Paste this into new AWS Lambda console and set you Forge key/secret below
* RESULT: Returns Tandem Access-token via js script file
* INCLUDE: <script src="https://xxx.lambda-url.us-west-2.on.aws"></script> in your index.html
* NOTE: Lambda Runtime must be Node v18+
*/
exports.handler = async (event) => {
const url = `https://developer.api.autodesk.com/authentication/v1/authenticate`;
const header = { 'Content-Type': 'application/x-www-form-urlencoded' }
const body = `grant_type=client_credentials&client_id=xxx&client_secret=xxx&scope=data:read`;
const json = await (await fetch( url, { method: 'POST', headers: header, body: body })).json();
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: `var _access_token = "${ json.access_token }";`
};
return response;
};
{
"name": "web-api-test",
"description": "Simple example that shows how to create WEB apis",
"private": true,
"files": [
"*.js"
],
"scripts": {
"start": "claudia create --name web-api-test --region us-east-1 --api-module web",
"deploy": "claudia update"
},
"devDependencies": {
"claudia": "^5"
},
"dependencies": {
"claudia-api-builder": "^4",
"node-fetch": "^2.6.1",
},
"version": "1.0.0",
"main": "web.js",
"license": "MIT"
}
// To install:
// 1. download the three files (web.js, package.json, claudia.js) locally
// > npm install
// 2. install claudia via https://github.com/claudiajs/example-projects/tree/master/web-serving-html
// 3. update the forge-client and secret in line 12: web.js
// To run:
// > claudia update
//
// Result:
// https://6jm6lvl74k.execute-api.us-west-2.amazonaws.com/latest/_adsk.js
//
// based on claudia web.js sample:
// https://github.com/claudiajs/example-projects/tree/master/web-serving-html
//
const fetch = require('node-fetch');
const ApiBuilder = require('claudia-api-builder');
const api = new ApiBuilder();
const url = `https://developer.api.autodesk.com/authentication/v1/authenticate`;
const header = { 'Content-Type': 'application/x-www-form-urlencoded' }
api.get('/_adsk.js', async () => {
const body = `grant_type=client_credentials&client_id=xxxx&client_secret=xxxx&scope=data:read`;
let token = await fetch( url, { method: 'POST', headers: header, body: body });
token = await token.json();
return `var _access_token = "${ token.access_token }";`;
},{
success: { contentType: 'text/html; charset=utf-8' },
error: {code: 403}
});
module.exports = api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment