How to use Twitter API to check if user / password is valid?

I have a user and password, and I would like to use the Twitter API (I use python-twitter) to check if this data is valid.

+3
source share
2 answers

If you take the python-twitter version , the VerifyCredentials method is added. If the return value is None, then the username and password combination was incorrect.

At first I liked it api.GetUser(username)to check if they provided a valid username, and then using api.VerifyCredentials()to check the password - this way I could tell the user how much of their login was incorrect.

As already mentioned, Twitter only clicks on OAuth logins and is expected to abandon the existing username / password login method.

0
source

urllib2 will raise an HTTPError if the credentials are incorrect and the message will be "HTTP Error 401: Unauthorized". You can use a temporary API instance to authorize the request (GetFriends (), for example) in the try / except block to determine if the credentials are valid.

import twitter, urllib2
checkCreds = twitter.Api(username, pass)
try:
  checkCreds.GetFriends()
except urllib2.HTTPError, status:
  if status.find('401') != -1:
    print 'Invalid username/password'

Twitter , GetPublicTimeline() GetUserTimeline().

+1

Source: https://habr.com/ru/post/1733729/


All Articles