【问题标题】:How to access Wordpress authentication token如何访问 Wordpress 身份验证令牌
【发布时间】:2019-09-10 00:42:59
【问题描述】:

我们正在尝试使用 OAuth 2.0 将我们的网站链接到 Wordpresses API。希望客户可以从我们的网站进行身份验证并发布到 WordPress。为此,我们需要接收访问令牌。

我们已成功连接到 Wordpress 以接收我们的访问代码。我们遵循了 Wordpress api,并让它为单个用户工作(使用密钥而不是 OAuth)。我们尝试过的事情是添加标题,将数据更改为不同的名称示例:参数、正文

这是我们一直在使用的代码的简化版本

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 访问令牌,但却收到了这个 400 错误:

data:
    { error: ‘invalid_client’,
      error_description: ‘The required “client_id” parameter is missing.’ } } }

很明显,我们缺少client_id,但我们已将其包含在我们的请求中。我们还需要在其他地方包含它吗?

【问题讨论】:

    标签: node.js wordpress express oauth-2.0 jwt


    【解决方案1】:
     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
          };
    

    我们需要包含包含该信息的标题,并且我们需要使用变量“form”而不是“data”。

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2019-04-28
      • 2014-12-17
      • 2014-03-12
      相关资源
      最近更新 更多