Breakdown how to connect to twitch API
Requirements
- Twitch account with Two-Factor Authentication
- Node.js
npm packages
- request 2.8 +
- async 3.2 +
1- Go to Twitch Devs website loging with your twitch account
2- Authorize developer service agreement
3- Go to Twitch Console
4- Click on register your application
5- Creating The application
You will need to give a name to the application then for OAuth Redirect URLs i am going to use http://localhost but if you have public ip and and a domain you can use it as well but for this guide we are going to use localhost Then click on create
6- Manage your application
Go to Twitch apps and click on manage at your application
7- Create new secret
Click on the new secret and save where it is secure
8- Getting the code access
You will need to use this url and change YOUR_CLIENT_ID you got in the step 6
https://id.twitch.tv/oauth2/authorize
?response_type=code
&client_id=<YOUR_CLIENT_ID>
&redirect_uri=http://localhost
&scope=chat%3Aread+chat%3Aedit
Also you can change the scope for your application for more information click here
Paste that url in your browser and press enter, it going to redirect you to this page and click on authorize
Then it will get you to localhost in this new page you are going to see your application code save it (the code is in the url)
9- Installed some requirements
In your machine create a folder with the name of your project, open cmd or your favorite terminal go to the directory you already create and run the next commands
node -v
npm -v
You are going to get the response of the version of your node and npm if you haven’t installed node js you can download here
Then run npm init and press enter until it says is this ok? (yes) and type yes
npm init
Press ^C at any time to quit.
package name: (dummy)
version: (1.0.0)
description:
entry point: (server.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to <Your PATH>
{
"name": "dummy",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes) yes
then you need to install the next packages
npm install async
npm install request
10- Getting the access code
Open your favorite code editor and paste the next code change the values client_id, client_secret and code for your values all the values need to be between apostrophe –> ‘’
const request = require("request");
const async = require("async");
const options = {
url: 'https://id.twitch.tv/oauth2/token',
json:true,
body: {
client_id: <YOUR_CLIENT_ID_HERE>,
client_secret: <YOUR_CLIENT_SECRET_HERE>,
code: <YOUR_CODE_HERE>,
grant_type: 'authorization_code',
redirect_uri: 'http://localhost',
}
};
request.post(options, (err,res,body)=>{
if(err){
return console.log(err);
}
console.log('Status: ${res.statusCode}');
console.log(body);
});
Now you save as code.js it and run it with in your terminal with node code.js
node code.js
And the output of the command will be something like this