I'm trying to create a simple Pocket web application that would connect to their API and first authenticate and then retrieve the bookmarks of the authenticated user. Unfortunately I'm having using connecting to their service since I'm really inexperienced in this field.
I fail at step #2 of the documentation's authentication process which is getting a request token.
What i got so far is:
var url = "https://getpocket.com/v3/oauth/request";
var data = "consumer_key=censored&request_uri=foo.bar/success";
function post(url, data, parameters){
var request;
try{
request = new XMLHttpRequest();
}catch(e){
request = false;
}
if(request !== false){
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// request.setRequestHeader("Access-Control-Allow-Origin", "*");
request.send(data);
request.onreadystatechange = function(){
console.log(request.responseText);
};
}else{
console.log("Couldn't create a new XmlHttpRequest Object");
}
}
post(url, data);
I know that modern browsers block cross domain requests but I'm not sure how to fix it without using a proxy. The documentation doesn't mention anything (or it did and i didn't understand how). I'm aware that via JSONP i could get around this issue but im not sure how to tell if their API supports that either (or how to implement it). They only mention 2 options:
- application/x-www-form-urlencoded (DEFAULT)
- application/json
This is the error im getting:
XMLHttpRequest cannot load https://getpocket.com/v3/oauth/request. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
edit
I just finished trying to create the json version to see if this one works but seems like I'm doing something wrong (again).
var url = "https://getpocket.com/v3/oauth/request";
var data = {};
data.comsumer_key = "censored";
data.request_uri = "foo.bar/success";
function post(url, data, parameters){
var request;
try{
request = new XMLHttpRequest();
}catch(e){
request = false;
}
if(request !== false){
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader("X-Accept", "application/json");
request.setRequestHeader("Access-Control-Allow-Origin", "*");
request.send(JSON.stringify(data));
request.onreadystatechange = function(){
console.log(request.responseText);
};
}else{
console.log("Couldn't create a new XmlHttpRequest Object");
}
}
post(url, data);
edit number 2
I uploaded a live demo here, It'd be easier to debug i guess