【问题标题】:Axios not working for accessing getpocket apiAxios 无法访问 getpocket api
【发布时间】:2018-09-16 18:08:29
【问题描述】:

我可以使用 curl 发出 oauth 请求,但使用 axios 我收到有关缺少使用者密钥的错误

X 错误 缺少使用者密钥。

这发生在发出请求的 OPTIONS 阶段。

 axios({
  method: 'post',
  url: 'https://getpocket.com/v3/oauth/request',
  data: {
    'consumer_key': 'xxxx-xxxxxxxxxxxxxxx',
    'redirect_uri': 'http://localhost:8080/'
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>

这是 curl 请求

curl https://getpocket.com/v3/oauth/request --insecure -X POST -H "Content-Type: application/json" -H "X-Accept: application/json" -d "{\"consumer_key\":\"xxxx-xxxxxx\",\"redirect_uri\":\"http://localhost:8080/\"}"

【问题讨论】:

  • 我敢打赌这是与 CORS 相关的。是 POST 本身收到该错误,还是有一个飞行前的 OPTIONS 请求收到错误?此外,可能错误消息是错误的,API 需要 X-Accept: application/json 标头,您的 axios 示例未使用该标头。
  • 运行上面的 sn-p 确认:“对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。来源 'null ' 因此不允许访问。"
  • 出现错误的是飞行前的 OPTIONS。如果是 CORS 错误,为什么 curl 会起作用?
  • CORS 只是一个浏览器的东西,所以从服务器或命令行执行它不需要 CORS。
  • 看起来你不是第一个遇到这个问题的人:stackoverflow.com/questions/35194986/…

标签: javascript api axios pocket


【解决方案1】:

尝试将标头添加到您的请求中以使其接受 json:

axios({
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json;charset=UTF-8'
    },
    url: 'https://getpocket.com/v3/oauth/request',
    data: {
        'consumer_key': 'xxxx-xxxxxxxxxxxxxxx',
        'redirect_uri': 'http://localhost:8080/'
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2021-10-23
    相关资源
    最近更新 更多