Search code examples
pythonapitwitterweb-scraping

Twitter API - How to get OAUTH_FILE?


I went to http://twitter.com/apps/new to create an app and get values for these credentials - CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN and OAUTH_TOKEN_SECRET.

Everything worked fine. However, when I tried to execute this script proposed by Matthew Russel.

import json
from flask import Flask, request
import multiprocessing
from threading import Timer
from IPython.display import IFrame
from IPython.display import display
from IPython.display import Javascript as JS

import twitter
from twitter.oauth_dance import parse_oauth_tokens
from twitter.oauth import read_token_file, write_token_file

OAUTH_FILE = "xx"

CONSUMER_KEY = 'xxxxx'
CONSUMER_SECRET = 'xxxxx'
oauth_callback = 'http://127.0.0.1:5000/oauth_helper'

I did not understand how to get OAUTH_FILE. Is it from twitter account as well ?


Solution

  • Try this:

    import base64
    
    def oAuth(KEY, SECRET):
        TOKEN_BYTES = bytes(str(KEY) + ':' + str(SECRET), encoding='utf-8')
        AUTH_TOKEN = base64.b64encode(TOKEN_BYTES).decode('utf-8')
        return AUTH_TOKEN
    
    OAUTH_FILE = oAuth(CONSUMER_KEY, CONSUMER_SECRET)