我在尝试运行代码时遇到发布请求身份验证问题,我收到错误 invalide request 有谁知道我如何解决这个问题,我收到此错误
{ error: 'invalid_request' }
const express = require('express');
const fetch = require('node-fetch');
const btoa = require('btoa');
const { catchAsync } = require('./utils');
const router = express.Router();
const CLIENT_ID = 'CLIENT_ID ';
const CLIENT_SECRET = 'CLIENT_SECRET';
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
const redirect = encodeURIComponent('http://localhost:50451/callback');
router.get('/login', (req, res) => {
res.redirect('https://flow.polar.com/oauth2/authorization?response_type=code&client_id=client_id');
});
router.get('/list', catchAsync(async (req, res) => {
if (!req.query.code) throw new Error('NoCodeProvided');
const code = req.query.code;
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
fetch('https://polarremote.com/v2/oauth2/token', {
method: 'POST',
data : {
'grant_type' : 'authorization_code',
'redirect_uri' : redirect,
'code' : code
},
headers : {
'Authorization':`Basic ${creds}`,
'Content-Type':'application/x-www-form-urlencoded',
'Accept':' application/json;charset=UTF-8'
}
})
.then(function(response) {
return response.json();
console.log(response.json());
}).then(function(body) {
console.log(body);
res.send('POST');
});
})
当我在 URI 中传递参数时,我遇到同样的问题,我的意思是
https://polarremote.com/v2/oauth2/token?grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
。当我使用 @PHP curl_setopt($connection, CURLOPT_POSTFIELDS, 'grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA');
时效果很好。