Processing
 

Login to Twitter using requests

25/08/2016 16:07#1

Joined at: 4 months ago

Post: 1

Thank: 0

Thanked: 0

I am trying to login to Twitter with requests module in Python (2.7), and after I am logged in, I want to visit another directory on Twitter.

So:

  1. Visit: https://twitter.com/login
  2. Locate username/pass elements
  3. Input Twitter Credentials
  4. Visit: https://twitter.com/settings/your_twitter_data
  5. Input Twitter Password

My code so far:

import requests import sys import os  #LOGIN data = {"session[username_or_email]":"MY_USERNAME",     "session[password]":"MY_PASSWORD"} r = requests.post("https://twitter.com/login/", data=data)  if ("success" in r.json()):     print "Logged in successfully!" else:     print "Failed to login!"     sys.exit(0)  #CHANGE URL AND INPUT PASSWORD data = {"auth_password":"MY_PASSWORD"} r = requests.post("https://twitter.com/settings/your_twitter_data", data=data)  if ("success" in r.json()):     print "Success!"     sys.exit(0) 

When run, this is the error I get:

Traceback (most recent call last):   File "twitter.py", line 10, in <module>     if ("success" in r.json()):   File "/usr/lib/python2.7/dist-packages/requests/models.py", line 808, in json     return complexjson.loads(self.text, **kwargs)   File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 516, in loads     return _default_decoder.decode(s)   File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 373, in decode     raise JSONDecodeError("Extra data", s, end, len(s)) simplejson.scanner.JSONDecodeError: Extra data: line 1 column 5 - line 1 column 81 (char 4 - 80) 
25/08/2016 16:14Top#2

Loïc

Member

Joined at: 10 months ago

Post: 5

Thank: 0

Thanked: 0

You are using r.json() though response is HTML, try r.text instead.

Also if you want to know you are successfully logged in, you'd rather check for your profile picture or something alike.

Then, you should use requests.Session(), more info there : http://docs.python-requests.org/en/master/user/advanced/

Similar articles

How do twitter giveaways work?

8 months ago - Reply: 2 - Views: 126

How may I get genuine followers on Twitter?

3 years ago - Reply: 33 - Views: 1,828

Login to Twitter sometime fail

6 months ago - Reply: 1 - Views: 23

Twitter doesn't accept PHP's cURL, but Linux'

1 year ago - Reply: 2 - Views: 90

Twitter OAuth on iOS

5 months ago - Reply: 1 - Views: 92

why am i getting an error on .verifyCredentials()

5 months ago - Reply: 1 - Views: 35

TwitteR setup_twitter_oauth() gives an error

5 months ago - Reply: 1 - Views: 29

Tweepy - Getting the author of a retweeted tweet

5 months ago - Reply: 0 - Views: 4

Retrieving data from twitter with JavaScript

5 months ago - Reply: 0 - Views: 2

Combined social media feed returning the wrong data

5 months ago - Reply: 0 - Views: 0

Django and Twitter request_token

5 months ago - Reply: 0 - Views: 0

Python HMAC hashed value encoding to base64

5 months ago - Reply: 1 - Views: 32

At the rate symbol in facebook

5 months ago - Reply: 0 - Views: 1

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re