Skip to content

Instantly share code, notes, and snippets.

@reganstarr
Last active May 24, 2018 21:48
Show Gist options
  • Save reganstarr/153968d6444b9281a9bc291277984be1 to your computer and use it in GitHub Desktop.
Save reganstarr/153968d6444b9281a9bc291277984be1 to your computer and use it in GitHub Desktop.
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);
});
@markdmill
Copy link

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:

We had trouble sending your test through. Please try again. Error:
2018-04-03T06:16:30.718Z 8c3e1353-3706-11e8-a5e6-d96b7cf2253e Task timed out after 1.00 seconds

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment