"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Publish an Article to Medium Using Python and the Medium API

How to Publish an Article to Medium Using Python and the Medium API

Published on 2024-09-03
Browse:406

Introduction

As someone who uses Obsidian to write articles, I often find myself needing to copy and format my content manually when publishing to Medium. This process can be time-consuming and repetitive, especially when dealing with Markdown files. To streamline my workflow, I decided to develop a Python script that automates the publication of Markdown files directly to Medium. In this article, I’m excited to share with you how to programmatically publish articles using the Medium API, making the process faster and more efficient.

Setting Up the Medium API

To interact with Medium’s API, you first need to generate an integration token. This token will allow your Python script to authenticate and perform actions on your behalf.

Steps to Generate an Integration Token:

  1. Go to your Medium Security and apps.
  2. Scroll down to the “Integration tokens” section.
  3. Click on “Get integration token.”
  4. Copy the generated token and keep it safe; you’ll need it for your script.

With the token in hand, you’re ready to start coding.

Getting user’s details and publications

Here’s the Python code you’ll be using to interact with the Medium API:

import requests  

# Replace these with your actual Medium integration token and file path  
MEDIUM_TOKEN = 'your_medium_integration_token'

headers = {  
    'Authorization': f'Bearer {MEDIUM_TOKEN}',  
    'Content-Type': 'application/json',  
    'Accept': 'application/json',  
    'host': 'api.medium.com',  
    'Accept-Charset': 'utf-8'  
}  
url = '''https://api.medium.com/v1/me'''  
response = requests.get(url=url, headers=headers)  

print('status_code is: ',response.status_code)  
print('response text:', response.json())  
print('userId:', response.json()['data']['id'])

Fetching User Information
When you run the script, it sends a request to Medium’s API to fetch your user information. The response includes details like your user ID, which is required to publish content.

Publishing an Article

Now that you’ve successfully retrieved your user ID from the Medium API, you can move on to publishing an article. The process involves sending a POST request to Medium’s API with the article content and some metadata.

import requests
import json

# Replace with your actual Medium integration token and user ID
MEDIUM_TOKEN = 'your_medium_integration_token'
USER_ID = 'your_user_id'

headers = {
    'Authorization': f'Bearer {MEDIUM_TOKEN}',
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'host': 'api.medium.com',
    'Accept-Charset': 'utf-8'
}

url = f'https://api.medium.com/v1/users/{USER_ID}/posts'

# Article content and metadata
data = {
    "title": "Your Article Title",
    "contentFormat": "markdown",  # Choose 'html', 'markdown', or 'plain'
    "content": "# Hello World!\nThis is my first article using the Medium API.",
    "tags": ["python", "api", "medium"],
    "publishStatus": "draft"  # Choose 'public' or 'draft'
}

# Sending the POST request
response = requests.post(url=url, headers=headers, data=json.dumps(data))

print('Status code:', response.status_code)
print('Response:', response.json())

Now you can head over to Medium to check your latest draft. Once you’ve confirmed that everything is formatted correctly, you can go ahead and publish it directly!


Explore more

How to Publish an Article to Medium Using Python and the Medium API

Luca Liu

Hello there! ? I'm Luca, a Business Intelligence Developer with passion for all things data. Proficient in Python, SQL, Power BI, Tableau, SAP Business Objects.

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

? Connect with me on LinkedIn

How to Publish an Article to Medium Using Python and the Medium API

Release Statement This article is reproduced at: https://dev.to/luca1iu/how-to-publish-an-article-to-medium-using-python-and-the-medium-api-50g2?1 If there is any infringement, please contact study_golang @163.comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3