Axios调用Twitch API POST返回400缺失client_id及GET请求示例咨询
问题1:POST请求传参报错原因
- 配置字段错误:Axios 中 POST 请求的请求体配置字段为
data,你代码中使用的body不属于 Axios 的合法配置项,参数不会被携带到请求中,这是触发missing client id报错的直接原因。 - 接口格式要求不匹配:Twitch OAuth2 令牌接口不支持JSON格式的请求体,仅支持
application/x-www-form-urlencoded格式的请求体,或者直接将参数放到URL查询字符串中。就算你把body改为data,直接传对象的话Axios会默认按application/json发送,还是会报错。 - 参数值错误:你代码中
client_secret字段值多写了Bearer前缀,client_secret只需填写你申请的客户端密钥原文即可,Bearer前缀仅用于后续业务接口的请求头Authorization字段。
正确的POST请求写法示例(使用params配置自动拼接查询参数,和你手动拼URL的效果一致):
axios({ method: 'post', url: 'https://id.twitch.tv/oauth2/token', params: { client_id: 'a', client_secret: 'b', grant_type: 'client_credentials' } }).then(response => { console.log(response) }).catch(error => { console.log(error) })
问题2:Twitch游戏榜单GET请求示例
axios({ method: 'get', url: 'https://api.twitch.tv/helix/games/top', headers: { 'Client-ID': 'a', 'Authorization': 'Bearer b' } }).then(response => { // 返回的游戏榜单数据存储在 response.data 中 console.log(response.data) }).catch(error => { console.log(error) })
内容的提问来源于stack exchange,提问作者Arsiki
相关产品推荐
相关产品推荐

