Tweet using Python


Before using Tweet in Python, we have to follow some steps.

Step1 − at first we must have a tweeter profile and then it has to be added with our mobile number.

First go to - Settings -> Add Phone -> Add number -> Confirm -> Save.

We have to follow these steps. Then turn off all text notifications.

Step2 − Set up a new app.

Follow − Twitter Apps -> Create New App -> Leave Callback URL empty -> Create Twitter application.

Then display message "Your application has been created. You review and adjust your application's settings".

Step3 − By default, the access of tweet is read-only. For sending out tweets, requires write permission.

Follow − Permissions tab -> what type of access does your application need? -> Choose Read and Write -> Update settings.

Display this kind of message "The permission settings have been successfully updated. It may take a moment for the changes to reflect."

Step4 − Take time to get the keys and access tokens.

Follow − Keys and Access Tokens tab.

Click Create my access token.

Display "Your application access token has been successfully generated"

Then verify that the access token/secret - and the read or write permission.

Step5 − last is to installtweety. And the command is pip install tweety.

Posting a simple tweet

Example code

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 17 06:00:58 2018
@author: Satyajit
"""
import tweepy 
# personal details 
my_consumer_key ="customer_key"
my_consumer_secret ="secret_no"
my_access_token ="token_no"
my_access_token_secret ="secret_token"
# authentication of consumer key and secret 
my_auth = tweepy.OAuthHandler(my_consumer_key, my_consumer_secret) 
# Authentication of access token and secret 
my_auth.set_access_token(my_access_token, my_access_token_secret) 
my_api = tweepy.API(my_auth)
my_api.update_status(status=”Hi All Of my Friends !!!!”)

Posting a media file

Example code

import tweepy 
# personal information 
my_consumer_key ="customer_key"
my_consumer_secret ="secret_no"
my_access_token ="token_no"
my_access_token_secret ="secret_token"
# Authentication 
my_auth = tweepy.OAuthHandler(my_consumer_key, my_consumer_secret) 
my_auth.set_access_token(my_access_token, my_access_token_secret) 
my_api = tweepy.API(my_auth) 
my_tweet ="tweet msg" 
my_image_path ="path of the image"
# To attach the media file 
my_status = my_api.update_with_media(my_image_path, my_tweet)  
my_api.update_status(my_status = my_tweet) 

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements