Post to Medium from 1Writer

Viticci published a wonderful reencounter with 1Writer this week on MacStories while, by sheer coincidence, I was working on this action for a colleague to plug 1Writer into Medium with their fresh API. This article will guide you through a couple of steps to get it working.

Integration Token

Your first task is to generate an integration token, a secret key the script will use to connect into your Medium account. Log into your Medium account, go to Settings and scroll until you find a section titled Integration Tokens, set a description and click the Get integration token button, you’ll get your key below, a long string of random characters.

Screen to generate integration tokens on Medium.

Avoid sharing your API token since anyone can publish into your account with it. In case you share your API token, revoke and create a new one.

Setting up the action

I’ll give you two ways to build your Post to Medium action:

  • The Easy Way: follow a step by step guide on Workflow and create a 1Writer action with the code it outputs;
  • The Hard Way: Install directly on 1Writer and configure the Javascript yourself.

The Easy Way

This action provides a step-by-step guide that will configure every part of the script for you. It will first take you to the Medium settings where you should copy the Integration Token we generated and return to Workflow. Then it will ask you everything it needs to configure, like if you want to add tags, publish as draft, etc. The action will place the Javascript in your clipboard and take you to 1Writer.

Build Post in Medium

A step-by-step guide to create the Post in Medium action for 1Writer.

Get Workflow

At 1Writer, tap the button with an ellipsis icon in the bottom-left corner, a modal panel will overlay the screen, tap the Add Action button, select the Add Javascript Action option and you should go to a window like this one:

Set a name, pick an icon, paste your code in the script field and you’re good to go.

The Hard Way

A toast to the brave ones that followed me into the lair of the danger feared across the seven kingdom named Javascript. Download the scaffold action to get started:

Post to Medium

Posts to Medium. Customized the hard way. You deserve a medal.

Get Action

I expect you to know a bit of Javascript to be here, if you don’t understand anything here, you should reconsider the step-by-step guide. The first thing to look at is the data variable. It holds all the configurations we’ll send to Medium later. This is how you can set each of them:

  • title: leave it as empty string. The script will ignore anything you put here.
  • contentFormat: accepts 2 strings: "markdown" and "html". You can also set as boolean, true or false, to be prompted before publishing.
  • content: grabs text from the editor, I wouldn’t play with this one if I were you.
  • publishStatus: Would you prefer your post to be public, a draft or unlisted? This is where you decide that. You can either set the value as a string or true to be asked before publishing.
  • license: The license of your piece. You can check all the licenses available here. Again, set to true to be asked before publishing.
  • tags: takes a comma-separated string, an array or true. The latter to be asked before publishing. Default is an empty array.
  • canonicalUrl: this is a reference url when you write someplace else and publish on medium. Set it to true to be asked for a url before publishing.

After you configure the data, look for this line:

var token = '',
    usrId = '',
    headers;

Set token to the integration token we got in the beginning of this article. Now we need your user ID. The action will get the user ID for you every time you publish in case you leave it blank, otherwise you can create an url to find out:

https://api.medium.com/v1/me?accessToken=yourtoken

Replace yourtoken with your integration token and open that url in Safari, you'll receive a JSON object. You want the value from data.id, a long random string just like your token.

Conclusion

I hope this workflow eases your Medium workflow on iOS. If you face any errors or bugs, it's dangerous to go alone. Take this and let me know of the issue so we can work it out together.