Last active
March 13, 2026 12:15
-
-
Save maciejmajewski/bf4df4342c832e2b9bc6dc1fe3d63159 to your computer and use it in GitHub Desktop.
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 crypto = require("crypto"); | |
| const OAuth = require("oauth-1.0a"); | |
| const consumerKey = bru.getEnvVar("oauth_consumer_key"); | |
| const consumerSecret = bru.getEnvVar("oauth_consumer_secret"); | |
| const accessToken = bru.getEnvVar("oauth_access_token"); | |
| const accessTokenSecret = bru.getEnvVar("oauth_access_token_secret"); | |
| const missingEnvVars = [ | |
| ["oauth_consumer_key", consumerKey], | |
| ["oauth_consumer_secret", consumerSecret], | |
| ["oauth_access_token", accessToken], | |
| ["oauth_access_token_secret", accessTokenSecret] | |
| ] | |
| .filter(([, value]) => !value) | |
| .map(([name]) => name); | |
| if (missingEnvVars.length > 0) { | |
| throw new Error( | |
| `Missing OAuth1 env vars: ${missingEnvVars.join(", ")}` | |
| ); | |
| } | |
| // Pre-request scripts run before Bruno prepares the final request. | |
| // Interpolate URL now so OAuth signs the resolved endpoint. | |
| const resolvedUrl = bru.interpolate(req.getUrl()); | |
| const method = String(req.getMethod() || "GET").toUpperCase(); | |
| const oauth = OAuth({ | |
| consumer: { | |
| key: consumerKey, | |
| secret: consumerSecret | |
| }, | |
| signature_method: "HMAC-SHA256", | |
| hash_function(baseString, key) { | |
| return crypto.createHmac("sha256", key).update(baseString).digest("base64"); | |
| } | |
| }); | |
| const authData = oauth.authorize( | |
| { | |
| url: resolvedUrl, | |
| method | |
| }, | |
| { | |
| key: accessToken, | |
| secret: accessTokenSecret | |
| } | |
| ); | |
| req.setHeader("Authorization", oauth.toHeader(authData).Authorization); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment