Forked from reganstarr/zapier_code_create_medium_post.js
Created
May 24, 2018 21:48
-
-
Save vipickering/f74e4875249ac6ddd7b3bb63a287d50e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mediumIntegrationToken = "REPLACE_WITH_YOUR_MEDIUM_INTEGRATION_TOKEN"; | |
// That's it. No need to edit anything else below. | |
// split the comma-separated tags string into an array then remove any whitespace | |
var tagsArray = input.medium_tags.split(','); | |
for (var i = 0; i < tagsArray.length; i++) { | |
tagsArray[i] = tagsArray[i].trim(); | |
} | |
var data = JSON.stringify({ | |
"title": input.medium_title, | |
"contentFormat": "html", | |
"content": input.medium_content, | |
"canonicalUrl": input.medium_url, | |
"publishStatus": input.medium_publish_status, | |
"tags": tagsArray | |
}); | |
// Look up the author id | |
fetch('https://api.medium.com/v1/me', { | |
method: 'GET', | |
headers: { | |
'Authorization': 'Bearer ' + mediumIntegrationToken | |
} | |
}) | |
.then(function(res) { | |
return res.json(); | |
}) | |
.then(function(body) { | |
return body.data.id; | |
}) | |
.then(function(authorId) { | |
// Now create the medium post | |
var apiUrl = 'https://api.medium.com/v1/users/' + authorId + '/posts'; | |
fetch(apiUrl, { | |
method: 'POST', | |
body: data, | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ' + mediumIntegrationToken | |
} | |
}) | |
.then(function(res) { | |
return res.json(); | |
}) | |
.then(function(body) { | |
var output = body.data; | |
callback(null, output); | |
}) | |
.catch(callback); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to allow sending to the drafts folder via publishStatus.
All simple and quick to do before publishing so pushing to drafts may be preferable.