{"library":"oauth-1.0a","title":"OAuth 1.0a Request Authorization","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install oauth-1.0a"],"cli":null},"imports":["import OAuth from 'oauth-1.0a'","const OAuth = require('oauth-1.0a')","import * as crypto from 'node:crypto';\n// ... in config:\nhash_function(base_string, key) {\n  return crypto.createHmac('sha1', key).update(base_string).digest('base64');\n}"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import * as crypto from 'node:crypto';\nimport OAuth from 'oauth-1.0a';\n\nconst consumerKey = process.env.OAUTH_CONSUMER_KEY ?? '';\nconst consumerSecret = process.env.OAUTH_CONSUMER_SECRET ?? '';\n\nconst oauth = OAuth({\n    consumer: { key: consumerKey, secret: consumerSecret },\n    signature_method: 'HMAC-SHA1',\n    hash_function(base_string, key) {\n        return crypto\n            .createHmac('sha1', key)\n            .update(base_string)\n            .digest('base64');\n    },\n});\n\nconst request_data = {\n    url: 'https://api.twitter.com/1.1/account/verify_credentials.json',\n    method: 'GET',\n    data: {},\n};\n\n// Example token (for user-specific requests)\nconst token = {\n    key: process.env.OAUTH_TOKEN_KEY ?? '',\n    secret: process.env.OAUTH_TOKEN_SECRET ?? ''\n};\n\nconst authorized_request = oauth.authorize(request_data, token);\n\n// To get the header for an HTTP client:\nconst headers = oauth.toHeader(authorized_request);\n\nconsole.log('Authorization Header:', headers.Authorization);\n// Example of how you would typically send the request with 'fetch' or similar:\n// fetch(request_data.url, {\n//   method: request_data.method,\n//   headers: {\n//     ...headers,\n//     'Content-Type': 'application/json' // Or other appropriate content type\n//   }\n// }).then(res => res.json()).then(data => console.log(data));\n\nconsole.log('OAuth authorization data generated successfully.');","lang":"typescript","description":"This quickstart demonstrates how to initialize the `oauth-1.0a` library for Node.js, configure a SHA1 hash function using `node:crypto`, and generate authorization data for a sample GET request. It shows how to obtain the authorization header required for sending authenticated requests to OAuth 1.0a services.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}