Last active
May 24, 2018 21:48
-
-
Save reganstarr/153968d6444b9281a9bc291277984be1 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_canonical_url, | |
"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
Thanks so much for writing this up! I've got it all working great, based on an RSS trigger, but I'm getting an error:
Any suggestions?