How I Automate My Medium Posts to Publish on Twitter

How I Automate My Medium Posts to Publish on Twitter

Published at - Nov 12, 2021

This tutorial is a solution to a problem that I am facing. I need the automation to automatically publish my medium posts to my Twitter handle, so I have created a simple Twitter bot using python to solve my problem.

In this tutorial, I am going to call the medium’s public API to fetch the latest posts published in JSON format using the Then we have to tweet it using thetweepy python package.

· Medium API to fetch latest posts

· Step 1: Installation & Import packages

· Step 2: Generate Twitter bot tokens

· Step 3: Authenticate to Twitter

· Step 4: Get medium posts

· Step 5: Tweet

· Step 6: Set Cron job

· Conclusion

Medium API to Fetch Latest Posts{username_here}

You have to replace username_here it with your username, in my case, it is @harendraverma21. It will provide JSON data of your posts.

Screenshot of JSON responseScreenshot of JSON response

We will use it letter in this tutorial.

Step 1: Install & Import Packages

Install packages using pip

pip install tweepy
pip install requests

Import into a python file

import tweepy

import urllib

from urllib.request import urlretrieve

from urllib.parse import urlparse

import os

import requests

Step 2: Generate Twitter Bot Tokens

You have to visit and create your app to get tokens. This app will require 4 keys —

API_KEY = "token_here"

API_SECRET_KEY = "token_here"

ACCESS_TOKEN = "token_here"

ACCESS_SECRET = "token_here"

You have to replace token_here with your token.

Step 3: Authenticate to Twitter

auth = tweepy.OAuthHandler(API_KEY,API_SECRET_KEY)


Step 4: Get Medium Posts

Now we have to retrieve posts from medium public API as mentioned above.

response = requests.get(url)

posts = response.json()

if posts["status"] == "ok":

    posts = posts["items"];

    for post in posts:

        post_title = post["title"]

        post_url = post["link"]

        post_image = post['thumbnail']

Step 5: Tweet

This is the final step to tweet your post on Twitter.

post_result = api.update_status(post_title)

I have provided a basic idea of how you can tweet your posts on Twitter. There are some bugs are in this code such as it will always tweet the latest post multiple times. To overcome the problem I have applied a little trick here. I will save the thumbnail with the post id of the post every time and always check tweet posts that don’t have images in our directory.

Let’s see the full code here:

<iframe src="" frameborder=0></iframe>

I have provided the gist of the code with proper comments which will help you to understand the code.

Step 6: Set Cron Job

To automate the process we have to set corn at every x min, to do that I am using crontab to schedule it in my system.

*/10 * * * * python /path_to_script/

This script will run every 10 minutes.


I hope this tutorial will help you solve the problem of tweeting each post to Twitter. It helps me a lot to save time. Feel free to ask anything related to this tutorial.

Thank you for reading this tutorial. Don’t forget to follow and Clap 👏 for this article.

Read more posts 5 Ways to Earn Money From Your Code Top ways to start earning money with your coding Major Differences between Python and R Programming Languages Python vs R: Learn the major differences between Python and R programming languages and get answers to some common… How to Create QR Code Generator and Reader Using Python A guide on creating and reading QR codes in Python with a working Top 10 Programming Languages to Learn in 2022 In the year 2022, below is a list of the top programming languages to The Best Python Compiler to Run Code Online Top online compilers for Python programming

More content at

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Related Posts -

How to Conduct CRUD Operations on SQLite Database Using Python
How to Conduct CRUD Operati ...

This post will teach you how to conduct CRUD operations on the SQLite datab...

The Best Python Compiler to Run Code Online
The Best Python Compiler to ...

If you have Python Compiler installed on your system and want to run a prog...

Introduction to Sets, Named Tuples and Deques in Python Programming
Introduction to Sets, Named ...

Introduction to Sets, Named Tuples and Deques in Python Programming Exampl...

Python 3 speech recognition (speech to text)
Python 3 speech recognition ...

Speech Recognition is an important feature in several applications used suc...

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile