master-zapier-plan-draft/vendor/zapier-platform/example-apps/oauth1-twitter/authentication.js
George Lambert b4150c8250 Milestone 0: import zappier billing, Verae middleware, and Zapier research
Compose-ready workspace: packages/zappier (rate card, portal, Stripe),
packages/verae-zapier-middleware (timestamp + NATS), packages/verae-zapier
(CLI app), vendor/zapier-platform, and research/zapier vendor corpus.

Gate 0 structure checks pass. Product code and research are not yet wired.
2026-09-09 02:37:36 -04:00

83 lines
2.4 KiB
JavaScript

const _ = require('lodash');
const REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token';
const AUTHORIZE_URL = 'https://api.twitter.com/oauth/authorize';
const ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token';
const getRequestToken = async (z, bundle) => {
const response = await z.request({
url: REQUEST_TOKEN_URL,
method: 'POST',
auth: {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_signature_method: 'HMAC-SHA1',
oauth_callback: bundle.inputData.redirect_uri,
oauth_version: '1.0', // Twitter says this should be 1.0
},
});
return response.data;
};
const getAccessToken = async (z, bundle) => {
const response = await z.request({
url: ACCESS_TOKEN_URL,
method: 'POST',
auth: {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_token: bundle.inputData.oauth_token,
oauth_token_secret: bundle.inputData.oauth_token_secret,
oauth_verifier: bundle.inputData.oauth_verifier,
},
});
return response.data;
};
const config = {
type: 'oauth1',
oauth1Config: {
// We have to define getRequestToken and getAccessToken functions to explicitly
// parse the response like it has a form body here, since Twitter responds
// 'text/html' for the Content-Type header
getRequestToken,
getAccessToken,
authorizeUrl: {
url: AUTHORIZE_URL,
params: {
oauth_token: '{{bundle.inputData.oauth_token}}',
},
},
},
test: {
url: 'https://api.twitter.com/1.1/account/settings.json',
},
connectionLabel: '{{screen_name}}',
};
// A middleware that is run before z.request() actually makes the request. Here we're
// adding necessary OAuth1 parameters to `auth` property of the request object.
const includeAccessToken = (req, z, bundle) => {
if (
bundle.authData &&
bundle.authData.oauth_token &&
bundle.authData.oauth_token_secret
) {
// Just put your OAuth1 credentials in req.auth, Zapier will sign the request for
// you.
req.auth = req.auth || {};
_.defaults(req.auth, {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_token: bundle.authData.oauth_token,
oauth_token_secret: bundle.authData.oauth_token_secret,
});
}
return req;
};
module.exports = {
config,
includeAccessToken,
};