Hi all and happy easter,
I am writing an application through Electron for a simple text editor that posts drafts to medium.com. They provide an api and the documentation for it, but my knowledge in jquery and js is still a little limited. Essentially, I'm using Ajax to post the data to medium, but receiving a 400 error. I'm sure it's something really dumb and simple, but I cant figure it out, so here's the code i've written to post the data:
$('.save_draft').click(function(){
var accessToken = 'xxxxx';
$.ajax({
url: "https://api.medium.com/v1/users/" + user.id + "/posts",
type: 'POST',
dataType: 'html',
contentType: "application/json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", accessToken)
},
data: {
"title": "Liverpool FC",
"contentFormat": "html",
"content": "<h1>Liverpool FC</h1><p>You’ll never walk alone.</p>",
"canonicalUrl": "http://jamietalbot.com/posts/liverpool-fc",
"tags": ["football", "sport", "Liverpool"],
"publishStatus": "draft",
},
success: function(data){
alert(data);
}
});
});
Now i'm providing the accessToken, i've just 'xxxxx'd it for posting. user.id is received at the start, I can confirm that it's coming through correctly. As for the documentation provided, you can see it here: https://github.com/Medium/medium-api-docs#33-posts but essentially it's asking for this:
POST /v1/users/5303d74c64f66366f00cb9b2a94f3251bf5/posts HTTP/1.1
Host: api.medium.com
Authorization: Bearer 181d415f34379af07b2c11d144dfbe35d
Content-Type: application/json
Accept: application/json
Accept-Charset: utf-8
{
"title": "Liverpool FC",
"contentFormat": "html",
"content": "<h1>Liverpool FC</h1><p>You’ll never walk alone.</p>",
"canonicalUrl": "http://jamietalbot.com/posts/liverpool-fc",
"tags": ["football", "sport", "Liverpool"],
"publishStatus": "public"
}
Like I say, i'm somewhat inexperienced with headers in ajax and a little confused so any help would be appreciated. Thanks.
The updated code:
$.ajax({
url: "https://api.medium.com:443/v1/users/" + user.data.id + "/posts",
type: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: JSON.stringify({
"title": "Liverpool FC",
"contentFormat": "html",
"content": "<h1>Liverpool FC</h1><p>You’ll never walk alone.</p>",
"canonicalUrl": "http://jamietalbot.com/posts/liverpool-fc",
"tags": ["football", "sport", "Liverpool"],
"publishStatus": "draft",
}),
success: function(data){
alert(data);
}
});
Aucun commentaire:
Enregistrer un commentaire