我正在嘗試從 Twitch 獲取 oauth 令牌,并使用以下代碼:
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))
在適當的地方使用我的秘密和客戶 ID。但是它不斷回傳:
{"status":400,"message":"missing client id"}
我究竟做錯了什么?
uj5u.com熱心網友回復:
您需要在發送之前對正文進行字串化,headers而不是header.
let request = {
method:'POST',
headers:{ "Content-Type": "application/json" },
body:JSON.stringify(creds)
};
另外,請res.json()改用因為您指定"Content-Type": "application/json"
fetch(process.env.AUTH_URL,request)
.then (res => res.json())
.then (text => console.log(text))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/398154.html
標籤:javascript 节点.js 抽搐 节点获取 twitch-api
上一篇:JS的this指向
