Created
December 29, 2017 08:51
Write articles on Medium without leaving command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
access_token = '181d415f34379af07b2c11d144dfbe35d' #Fake token, obviously! | |
headers = { | |
'Authorization': "Bearer " + access_token, | |
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84' | |
} | |
base_url = "https://api.medium.com/v1/" | |
# Validate access token by making a call to https://api.medium.com/v1/me | |
me_response = requests.request("GET", base_url + 'me', headers=headers).text | |
json_me_response = json.loads(me_response) | |
print(json_me_response) | |
user_id = json_me_response['data']['id'] | |
user_url = base_url + 'users/' + user_id + '/' | |
# Get all the publications corresponding to a user | |
publications_response = requests.request("GET", user_url + 'publications/', headers=headers).text | |
print(publications_response) # Would be an empty array for me | |
# Publish new post | |
posts_url = user_url + 'posts/' | |
print(posts_url) | |
payload = { | |
'title': 'Medium Test Post', | |
'contentFormat': 'markdown', | |
'tags': ['medium', 'test', 'python'], | |
'publishStatus': 'draft', | |
'content': open('7.Test_post.md').read() | |
} | |
response = requests.request('POST', posts_url, data=payload, headers=headers) | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like the new tokens are no longer accepted, medium has closed the API. Using my token I get Application not found on https://api.medium.com/v1/me