1

I can make the oauth request using curl but using axios I get a error about missing consumer key

X-Error Missing consumer key.

This happens in the OPTIONS phase of making the request.

 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>

Here is the curl request

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/\"}"
KahunaCoder
  • 615
  • 7
  • 14
  • I bet this is CORS-related. Is it the `POST` itself that is getting that error, or is there a pre-flight `OPTIONS` request that is getting the error? Also, maybe the error message is bad and the API _requires_ that `X-Accept: application/json` header, which your axios example isn't using. – Jacob Sep 16 '18 at 18:17
  • Running that snippet above confirms: "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access." – Jacob Sep 16 '18 at 18:18
  • It's the pre-flight OPTIONS that is getting the error. If it's a CORS error why does curl work? – KahunaCoder Sep 16 '18 at 18:19
  • CORS is just a browser thing, so executing this from a server or from a command line won't require CORS. – Jacob Sep 16 '18 at 18:20
  • Looks like you aren't the first running across this: https://stackoverflow.com/questions/35194986/setting-headers-and-sending-data-with-http-post-to-pocket-api-returns-cors – Jacob Sep 16 '18 at 18:21
  • Thanks @Jacob I guess I can't do what I wanted without a server. – KahunaCoder Sep 16 '18 at 18:49

1 Answers1

0

Try adding the headers to your request for it to accept 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/'
    }
});
c-chavez
  • 7,237
  • 5
  • 35
  • 49