OAuth 1.0a Request Authorization
oauth-1.0a is a JavaScript library providing a streamlined way to authorize requests using the OAuth 1.0a protocol in both Node.js and browser environments. It abstracts away the complexities of generating `oauth_consumer_key`, `oauth_nonce`, `oauth_signature`, and other OAuth 1.0a parameters, allowing developers to use their preferred HTTP client (e.g., `request`, `jQuery.ajax`). The current stable version is 2.2.6, with minor updates addressing dependency bumps and TypeScript type improvements. A key differentiator is its separation of cryptographic hashing, requiring users to provide a `hash_function` implementation, which allows for flexibility with native Node.js `crypto` or browser-specific libraries like CryptoJS. It aims to simplify integration with popular OAuth 1.0a services like Twitter, Flickr, and Bitbucket.
Common errors
-
TypeError: (0, _oauth10a.OAuth) is not a function
cause Attempting to import `OAuth` as a named export when it is a default export in an ESM context.fixChange `import { OAuth } from 'oauth-1.0a'` to `import OAuth from 'oauth-1.0a'`. -
Error: Cannot find module 'crypto'
cause The `node:crypto` module is not available in the current Node.js environment or bundler setup.fixVerify that your Node.js installation includes the crypto module. If using a bundler (like Webpack for browser), ensure `node:crypto` is correctly polyfilled or stubbed, or provide a browser-specific `hash_function`. -
Error: Missing consumer key or secret.
cause The `consumer.key` or `consumer.secret` properties were not provided or were empty strings during `OAuth` initialization.fixEnsure that `consumer.key` and `consumer.secret` are valid, non-empty strings in the configuration object passed to `OAuth()`.
Warnings
- breaking Version 2.0.0 introduced breaking changes: `consumer.public` was renamed to `consumer.key`, and the `CryptoJS` dependency was removed. Users must now provide their own `hash_function` implementation.
- gotcha The `crypto` module in Node.js is not guaranteed to be present in all environments, especially in highly customized or restricted Node.js builds. If `require('crypto')` throws an error, the native crypto module is unavailable.
- gotcha For browser usage, you *must* explicitly provide a browser-compatible cryptographic hashing library (e.g., Google's CryptoJS) as the `hash_function`. The library does not bundle one.
- gotcha The `oauth-1.0a` package exports a default function. Using named imports like `import { OAuth } from 'oauth-1.0a'` will result in `undefined` for `OAuth` in ESM environments.
Install
-
npm install oauth-1.0a -
yarn add oauth-1.0a -
pnpm add oauth-1.0a
Imports
- OAuth
import { OAuth } from 'oauth-1.0a'import OAuth from 'oauth-1.0a'
- OAuth (CommonJS)
const OAuth = require('oauth-1.0a') - hash_function (HMAC-SHA1)
import { createHmac } from 'crypto'; // Not always safe or cross-platform.import * as crypto from 'node:crypto'; // ... in config: hash_function(base_string, key) { return crypto.createHmac('sha1', key).update(base_string).digest('base64'); }
Quickstart
import * as crypto from 'node:crypto';
import OAuth from 'oauth-1.0a';
const consumerKey = process.env.OAUTH_CONSUMER_KEY ?? '';
const consumerSecret = process.env.OAUTH_CONSUMER_SECRET ?? '';
const oauth = OAuth({
consumer: { key: consumerKey, secret: consumerSecret },
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto
.createHmac('sha1', key)
.update(base_string)
.digest('base64');
},
});
const request_data = {
url: 'https://api.twitter.com/1.1/account/verify_credentials.json',
method: 'GET',
data: {},
};
// Example token (for user-specific requests)
const token = {
key: process.env.OAUTH_TOKEN_KEY ?? '',
secret: process.env.OAUTH_TOKEN_SECRET ?? ''
};
const authorized_request = oauth.authorize(request_data, token);
// To get the header for an HTTP client:
const headers = oauth.toHeader(authorized_request);
console.log('Authorization Header:', headers.Authorization);
// Example of how you would typically send the request with 'fetch' or similar:
// fetch(request_data.url, {
// method: request_data.method,
// headers: {
// ...headers,
// 'Content-Type': 'application/json' // Or other appropriate content type
// }
// }).then(res => res.json()).then(data => console.log(data));
console.log('OAuth authorization data generated successfully.');