【问题标题】:{"status":400,"message":"missing client id"} response when using node-fetch{"status":400,"message":"missing client id"} 使用节点获取时的响应
【发布时间】:2021-12-30 14:49:40
【问题描述】:

我正在尝试从 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"}

我做错了什么?

【问题讨论】:

  • 请确认:console.log(process.env.CLIENT_ID) 中有内容。如果是,请使用环境变量 NODE_DEBUG=http 运行您的程序以查看发出的请求。

标签: javascript node.js twitch node-fetch twitch-api


【解决方案1】:

您需要在发送之前对正文进行字符串化,并使用headers 而不是header

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

另外,也可以使用res.json(),因为返回数据可能是json

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

【讨论】:

  • 得到了和以前一样的错误。
  • @user992286, headers 而不是 header。查看更新。
猜你喜欢
  • 2023-04-05
  • 2022-01-04
  • 1970-01-01
  • 2017-07-01
  • 2022-07-30
  • 2023-03-24
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多