npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@moaazsidat/twitter-v1-oauth

v1.0.15

Published

OAuth 1.0a authorization header for Twitter API

Downloads

366

Readme

Twitter OAuth 1.0a 🔑

Forked and updated to use crypto-browserify for usage on browser compute environments such as Cloudflare Workers

npm version Build and Test codecov CodeFactor

Simple and minimalist module to generate oAuth1.0a authorization header for Twitter API v1.1 and V2.

import dotenv from "dotenv";
dotenv.config();

import axios from "axios";
import oAuth1a from "twitter-v1-oauth";

const oAuthOptions = {
  api_key: process.env.TWITTER_API_KEY || "",
  api_secret_key: process.env.TWITTER_API_SECRET_KEY || "",
  access_token: process.env.TWITTER_ACCESS_TOKEN || "",
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET || "",
};
const url = "https://api.twitter.com/1.1/search/tweets.json";
const method = "GET";
const params = { q: "twitter bot" };

const authorization = oAuth1a({ method, url, params }, oAuthOptions);

axios
  .get(url, {
    params,
    headers: {
      authorization,
    },
  })
  .then(({ data }) => console.log(data))
  .catch((err) => {
    if (err.response) {
      return console.log(err.response.data.errors);
    }
    console.log(err);
  });

No dependencies and super small: install size.

twitter-v1-oauth

Install

npm install twitter-v1-oauth

Usage

Create an app and get your credentials, you will need:

  • API KEY
  • API SECRET KEY
  • ACCESS TOKEN
  • ACCESS TOKEN SECRET

Use your preferred library to send the request using the documented endpoints and parameters for the twitter v1 API.

Twitter API v1.1 vs V2

When making a post request to Twitter API v1.1, the data needs to be encoded and sent as application/x-www-form-urlencoded. The module exports an encode function that can be used to properly encode the body before it is send. Check post-tweet for an example.

Whe making a post request to Twitter API v2, the data doesn't need to be encoded and must be sent as application/json. Check like-v2 for an example.

CommonJS

const authRequest = require("twitter-v1-oauth").default;

ES6 Modules

import oAuthRequest from "twitter-v1-oauth";

TypeScript

Type definitions are included.

Examples

Check the examples directory for ideas on how to use it with axios.

The index.test.ts file should also provide a good idea on its usage.

Twitter documentation

License

MIT