Search code examples
pythontwittersqlitetwython

How to pass the values recieved from sqlite3 to Twython object in Python


I have a sqlite3 DB with Consumer Key, Consumer Secret, Access Token, Access Token Secret.

I'm SELECT'ing all the data and iterating through the cursor. While printing I get the data, but for some reason can't pass it to the twython object.

This is the first code I tried:

#!/usr/bin/env pythons
from twython import Twython
from time import sleep
import sqlite3

db = sqlite3.connect('./mydb')
cursor = db.cursor()

cursor.execute ('''SELECT app_key, app_secret, oauth_token, oauth_token_secret FROM users''')

for row in cursor:
    twitter = Twython(row[0], row[1], row[2], row[3])
    search = twitter.search(q = '#peace', count = 10)
    tweets = search['statuses']
    for tweet in tweets:
        b = tweet['id']
        print(b)
        twitter.retweet(id = b)
        sleep(60)

db.close()

Then I tried this:

#!/usr/bin/env pythons
from twython import Twython
from time import sleep
import sqlite3

db = sqlite3.connect('./mydb')
cursor = db.cursor()

cursor.execute ('''SELECT app_key, app_secret, oauth_token, oauth_token_secret FROM users''')

for row in cursor:
    twitter = Twython('{0}, {1}, {2}, {3}'.format(row[0], row[1], row[2], row[3]))
    search = twitter.search(q = '#peace', count = 10)
    tweets = search['statuses']
    for tweet in tweets:
        b = tweet['id']
        print(b)
        twitter.retweet(id = b)
        sleep(60)

db.close()

The error code:

twython.exceptions.TwythonAuthError: Twitter API returned a 400 (Bad Request), Bad Authentication data.

As an additional step, I tried manually passing the credentials (Consumer Key, Consumer Secret, Access Token, Access Token Secret) and it works like charm!

Can someone tell me what I'm doing wrong here? According to me the first code should work just fine!

Thanks in advance!


Solution

  • Apparently, the issue was caused because of white-spaces around the string.

    I solved this issue by using the strip() function to strip off all unnecessary white-space from the returned string!

    The current code:

    #!/usr/bin/env pythons
    from twython import Twython
    from time import sleep
    import sqlite3
    
    db = sqlite3.connect('./mydb')
    cursor = db.cursor()
    
    cursor.execute ('''SELECT app_key, app_secret, oauth_token, oauth_token_secret FROM users''')
    
    def stripStr(str1,str2,str3,str4):
        return str1.strip(), str2.strip(), str3.strip(), str4.strip()
    
    for row in cursor:
        app_key, app_secret, oauth_token, oauth_token_secret = stripStr(user[0], user[1], user[2], user[3])
        twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
        search = twitter.search(q = '#peace', count = 10)
        tweets = search['statuses']
        for tweet in tweets:
            b = tweet['id']
            print(b)
            twitter.retweet(id = b)
            sleep(60)
    
    db.close()
    

    After a day of pondering, it finally works! <3