Home > Article > Backend Development > How to Publish an Article to Medium Using Python and the Medium API
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.
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:
With the token in hand, you’re ready to start coding.
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.
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!
Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.
? Connect with me on LinkedIn
The above is the detailed content of How to Publish an Article to Medium Using Python and the Medium API. For more information, please follow other related articles on the PHP Chinese website!