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

WBOY
Release: 2024-08-31 22:31:03
Original
177 people have browsed it

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'])
Copy after login

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())
Copy after login

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

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!