Automate Your Writing: Publishing to Medium with Python and the Medium API
2024-9-4 00:38:8 Author: hackernoon.com(查看原文) 阅读量:0 收藏

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!


Thank you for taking the time to explore data-related insights with me. I appreciate your engagement. If you find this information helpful, I invite you to follow me or connect with me on LinkedIn. Happy exploring!👋


文章来源: https://hackernoon.com/automate-your-writing-publishing-to-medium-with-python-and-the-medium-api?source=rss
如有侵权请联系:admin#unsafe.sh