401尝试使用Oauth 1.0在Wordpress API中创建帖子时

时间:2017-07-25 14:29:15

标签: wordpress post oauth request axios

我试图通过Wordpress API v2创建Wordpress帖子,但是当我执行axios.post时,oauth 1.0向我投掷了401。当我做axios.get时,一切都很完美,我得到了结果。

我可以通过Postman创建或删除帖子没有问题,但它会自动配置。如果我能以某种方式从邮递员那里复制请求并将其放入axios代码中,那就太好了。但是找不到这个选项。

我尝试将content-type的标题指定为application / json,如下所示:

headers: {
    'Content-Type': 'application/json'
}

就像邮差一样,但仍然没有变化。

我使用生成器进行Oauth签名,并且正如指出的那样在GET请求中工作。 https://www.npmjs.com/package/oauth-signature

这里是get和post请求的代码:

getRequest = () => {
    const requestParams = { ...this.state.parameters }
    requestParams.oauth_nonce = this.generateNonce()
    requestParams.oauth_timestamp = new Date()
      .getTime()
      .toString()
      .slice(0,10)
    const encodedSignature = oauthSignature.generate(
      'GET',
      'http://localhost/wordpress-api/wp-json/wp/v2/posts/29',
      requestParams,
      this.state.consumerSecret,
      this.state.tokenSecret
    )

    axios({
      url: 'http://localhost/wordpress-api/wp-json/wp/v2/posts/29',
      method: 'get',
      auth: `
        OAuth oauth_consumer_key="${requestParams.oauth_consumer_key}",
        oauth_token="${requestParams.oauth_token}",
        oauth_signature_method="${requestParams.oauth_signature_method}",
        oauth_timestamp="${requestParams.oauth_timestamp}",
        oauth_nonce="${requestParams.oauth_nonce}",
        oauth_version="${requestParams.oauth_version}",
        oauth_signature="${encodedSignature}"
      `
    })
      .then(res => {
        this.setState({
          requestResponse: res
        })
      })
  }

postRequest = (e) => {
    e.preventDefault()

    const postData = {
      title: this.refs.title.value,
      status: 'publish',
      content: this.refs.content.value,
    }

    const requestParams = { ...this.state.parameters }
    requestParams.oauth_nonce = this.generateNonce()
    requestParams.oauth_timestamp = new Date()
      .getTime()
      .toString()
      .slice(0,10)
    const encodedSignature = oauthSignature.generate(
      'POST',
      'http://localhost/wordpress-api/wp-json/wp/v2/posts',
      requestParams,
      this.state.consumerSecret,
      this.state.tokenSecret
    )

    axios({
      url: 'http://localhost/wordpress-api/wp-json/wp/v2/posts',
      method: 'post',
      data: postData,
      auth: `
        OAuth oauth_consumer_key="${requestParams.oauth_consumer_key}",
        oauth_token="${requestParams.oauth_token}",
        oauth_signature_method="${requestParams.oauth_signature_method}",
        oauth_timestamp="${requestParams.oauth_timestamp}",
        oauth_nonce="${requestParams.oauth_nonce}",
        oauth_version="${requestParams.oauth_version}",
        oauth_signature="${encodedSignature}"
      `
    })
      .then(res => {
        this.setState({
          requestResponse: res
        })
      })
  }

1 个答案:

答案 0 :(得分:4)

最后,我弄清楚了问题是什么。如果Wordpress API有更多的例子,那就太好了。我的授权标题未正确设置。

以下是应该在Wordpress API的任何Oauth 1.0安全请求中使用的更新代码(GET,POST,PUT,DELETE,只需用'post'函数中的任何方法替换oauthSignature.generate()和{ {1}}请求)。检查并正常工作。

请记住,这只是组件状态中所有令牌和秘密的示例。您应该将这些存储在后端,并在提供一些凭据后才将它们传递给前端。

我上传了整个React组件代码,因为我已经很难理解互联网上有多少小的,无法使用的代码片段而不是整个解决方案,这让我非常恼火。对于那些想要有一个工作实例的人来说,这应该更具说明性。

axios