我们正在尝试使用 OAuth 2.0 将我们的网站链接到 Wordpresses API。希望客户能够从我们的网站进行身份验证并发布到 WordPress。我们需要接收访问 token 才能执行此操作。
我们已成功连接 Wordpress 以接收我们的访问代码。我们遵循 Wordpress api,并让它为单个用户工作(使用 key 而不是 OAuth)。我们尝试过添加 header 、将数据更改为不同的名称示例:params、body
这是我们一直使用的代码的简化版本
const axios = require('axios');
axios({
method: "POST",
data: {
grant_type: 'authorization_code',
client_id: '12345',
client_secret: 'ABCABC1235412345',
code: 'Abc123',
redirect_uri: 'https://localhost:5000/wordpress/callback_wordpress'
},
url: 'https://public-api.wordpress.com/oauth2/token'
}).then( (response) => {
console.log(response);
}).catch( (error) => {
console.log(error);
});
我们期望收到 jwt 访问 token ,但却收到此 400 错误:
data:
{ error: ‘invalid_client’,
error_description: ‘The required “client_id” parameter is missing.’ } } }
很明显我们缺少 client_id
,但我们已将其包含在我们的请求中。我们还需要在其他地方包含它吗?
最佳答案
var authOptions = {
url: 'https://public-api.wordpress.com/oauth2/token',
form:
{
grant_type: 'authorization_code',
code: code,
client_id: client_id,
client_secret: client_secret,
redirect_uri: redirect_uri,
},
headers: {
'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64'))
},
json: true
};
我们需要包含包含该信息的 header ,并且需要使用变量“form”而不是“data”。
关于node.js - 如何访问 WordPress 身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55767014/