0

I am trying to get an oauth token from Twitch, and with this code:

import dotenv from 'dotenv';
dotenv.config();
import fetch from 'node-fetch';

let creds = {
        client_id:process.env.CLIENT_ID,
        client_secret:process.env.CLIENT_SECRET,
        grant_type:"client_credentials"
};

let request = {
    method:'POST',
    header:{ "Content-Type": "application/json" },
    body:creds
};
console.log(request);

fetch(process.env.AUTH_URL,request)
    .then (res => res.text())
    .then (text => console.log(text))

With my secret and client id where appropriate. However it keeps returning:

{"status":400,"message":"missing client id"}

What am I doing wrong?

user992286
  • 295
  • 1
  • 3
  • 6
  • Please verify there is something in: `console.log(process.env.CLIENT_ID)`. If yes, run your program with the env variable `NODE_DEBUG=http` to see the request made. – Daniel W. Dec 30 '21 at 16:11

1 Answers1

2

You need to stringify the body before sending it and headers instead of header.

let request = {
    method:'POST',
    headers:{ "Content-Type": "application/json" },
    body:JSON.stringify(creds)
};

Also, maybe use res.json() instead because the return data maybe json.

fetch(process.env.AUTH_URL,request)
    .then (res => res.json())
    .then (text => console.log(text))
ikhvjs
  • 5,316
  • 2
  • 13
  • 36