Last active
March 5, 2025 05:39
-
-
Save surajp/1c49f3428832da95d2023b75ee264db1 to your computer and use it in GitHub Desktop.
Get access token in Postman from the Salesforce cli via a node server
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
{ | |
"info": { | |
"_postman_id": "35d1388c-2a10-4698-9cd6-6fb250c2b458", | |
"name": "Local SF Auth", | |
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", | |
"_exporter_id": "1474473", | |
"_collection_link": "https://team-sf.postman.co/workspace/Salesforce~e589a405-8419-4e78-9608-343f0d307f55/collection/1474473-35d1388c-2a10-4698-9cd6-6fb250c2b458?action=share&source=collection_link&creator=1474473" | |
}, | |
"item": [ | |
{ | |
"name": "Get Token From CLI", | |
"event": [ | |
{ | |
"listen": "test", | |
"script": { | |
"exec": [ | |
"pm.test(\"Set Access Token\", function () {", | |
" var jsonData = pm.response.json();", | |
" pm.expect(jsonData.accessToken).to.not.be.undefined;", | |
" pm.environment.set(\"_accessToken\", jsonData.accessToken);", | |
" pm.environment.set(\"_endpoint\",jsonData.instanceUrl);", | |
"});" | |
], | |
"type": "text/javascript", | |
"packages": {} | |
} | |
} | |
], | |
"protocolProfileBehavior": { | |
"disabledSystemHeaders": { | |
"accept": true | |
} | |
}, | |
"request": { | |
"auth": { | |
"type": "bearer", | |
"bearer": [ | |
{ | |
"key": "token", | |
"value": "someauthtoken", | |
"type": "string" | |
} | |
] | |
}, | |
"method": "GET", | |
"header": [ | |
{ | |
"key": "Accept", | |
"value": "application/json", | |
"type": "text" | |
} | |
], | |
"url": { | |
"raw": "http://localhost:{{port}}?username={{username}}", | |
"protocol": "http", | |
"host": [ | |
"localhost" | |
], | |
"port": "{{port}}", | |
"query": [ | |
{ | |
"key": "username", | |
"value": "{{username}}" | |
} | |
] | |
} | |
}, | |
"response": [] | |
} | |
], | |
"variable": [ | |
{ | |
"key": "port", | |
"value": "", | |
"type": "default" | |
} | |
] | |
} |
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
import { exec } from "child_process"; | |
import http from "http"; | |
http | |
.createServer((req, res) => { | |
if (req.headers.authorization !== "Bearer someauthtoken") { | |
res.writeHead(401).end(); | |
return; | |
} | |
const username = new URL(req.url, "http://localhost").searchParams.get( | |
"username", | |
); | |
exec(`sf org display -o ${username} --json`, (err, stdout) => { | |
if (err) | |
return res | |
.writeHead(500) | |
.end(JSON.stringify({ error: "CLI execution failed" })); | |
try { | |
const result = JSON.parse(stdout); | |
res.setHeader("Content-Type", "application/json"); | |
res.writeHead(200).end(JSON.stringify(result.result)); | |
} catch (e) { | |
res.writeHead(500).end(JSON.stringify({ error: "JSON parse error" })); | |
} | |
}); | |
}) | |
.listen(process.env.PORT || 8787, () => { | |
console.log("Server is listening on port " + (process.env.PORT || 8787)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment