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

@callowcreation/basic-twitch-oauth

v2.0.5

Published

Intended to reduce the expected prerequisite experience required for implementing Twitch OAuth to use the Twitch API(s)

Downloads

43

Readme

Basic Twitch OAuth Flow

Intended to reduce the expected prerequisite experience required for implementing Twitch OAuth to use the Twitch API(s)

Motivation

The statements below are still a bit beyond your experiences and you need/want to use a Twitch API… is my motivation.

Twitch API(s) now require that some endpoint request must have an authorization in the header. To acquire an access token for a Twitch API you must be familiar with an authentication flow. The flows supported by Twitch are Implicit code, authorization code, and client credentials.

See Twitch Authentication Docs

Features

  • Quick setup and usage.

Installation

Create an App

Required reading Getting Started with the Twitch API

$ npm install @callowcreation/basic-twitch-oauth

Loading and configuration

A Node server is required, express is used here.


const express = require('express');
const crypto = require('crypto');
const qs = require('querystring');

const app = express();

const buffer = crypto.randomBytes(16);
const state = buffer.toString('hex');

const twitchOAuth = new TwitchOAuth({
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    redirect_uri: process.env.REDIRECT_URI,
    scopes: [
        'user:edit:broadcast',
		'moderation:read'
    ]
}, state);

// redirect_uri ends up here
app.get('/auth-callback', async (req, res) => {
    const qs = require('querystring');
    const req_data = qs.parse(req.url.split('?')[1]);
    const code = req_data['code'];
    const state = req_data['state'];

	try {
		twitchOAuth.confirmState(state);
		await twitchOAuth.fetchToken(code);
		res.redirect('/home');
	} catch (err) {
		console.error(err);
		res.redirect('/failed');
	}
	
});

Common Usage

app.get('/extensions', async (req, res) => {
	const url: string = `https://api.twitch.tv/helix/users/extensions?user_id=${broadcaster_id}`;
	const json = await twitchOAuth.getEndpoint(url);
	res.status(200).json(json);
});

Handling exceptions

try {
	const url: string = `https://api.twitch.tv/helix/moderation/enforcements/status?broadcaster_id=${broadcaster_id}`;
	const data = [
		{ msg_id: '0', msg_text: 'I killing this', user_id: '101223367' },
		{ msg_id: '1', msg_text: 'that was a death blow', user_id: '75987197' }
	];
	const json = await twitchOAuth.postEndpoint(url, { data });
	res.status(200).json(json);
} catch (error) {
	console.error(error); 
	res.send(error_message); 
}

Contact

License

MIT