Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Rewritten get_access_token.py to use requests-oauth
Browse files Browse the repository at this point in the history
get_access_token.py now uses the requests oauth lib and no longer
depends on oauth2. This closes #151
  • Loading branch information
sebastianw committed Jul 9, 2014
1 parent 0424031 commit a3f8ff2
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions get_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# parse_qsl moved to urlparse module in v2.6
try:
from urlparse import parse_qsl
except:
from cgi import parse_qsl

import webbrowser
import oauth2 as oauth
from requests_oauthlib import OAuth1Session

REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
Expand All @@ -31,50 +25,48 @@

def get_access_token(consumer_key, consumer_secret):

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
oauth_client = oauth.Client(oauth_consumer)
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret)

print 'Requesting temp token from Twitter'

resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST', body="oauth_callback=oob")

if resp['status'] != '200':
print 'Invalid respond from Twitter requesting temp token: %s' % resp['status']
else:
request_token = dict(parse_qsl(content))
url = '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token'])

print ''
print 'I will try to start a browser to visit the following Twitter page'
print 'if a browser will not start, copy the URL to your browser'
print 'and retrieve the pincode to be used'
print 'in the next step to obtaining an Authentication Token:'
print ''
print url
print ''

webbrowser.open(url)
pincode = raw_input('Pincode? ')

token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(pincode)

print ''
print 'Generating and signing request for an access token'
print ''

oauth_client = oauth.Client(oauth_consumer, token)
resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_callback=oob&oauth_verifier=%s' % pincode)
access_token = dict(parse_qsl(content))

if resp['status'] != '200':
print 'The request for a Token did not succeed: %s' % resp['status']
print access_token
else:
print 'Your Twitter Access Token key: %s' % access_token['oauth_token']
print ' Access Token secret: %s' % access_token['oauth_token_secret']
print ''
try:
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting temp token: %s' % e
return
url = oauth_client.authorization_url(AUTHORIZATION_URL)

print ''
print 'I will try to start a browser to visit the following Twitter page'
print 'if a browser will not start, copy the URL to your browser'
print 'and retrieve the pincode to be used'
print 'in the next step to obtaining an Authentication Token:'
print ''
print url
print ''

webbrowser.open(url)
pincode = raw_input('Pincode? ')


print ''
print 'Generating and signing request for an access token'
print ''

oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
resource_owner_key=resp.get('oauth_token'),
resource_owner_secret=resp.get('oauth_token_secret'),
verifier=pincode
)
try:
resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting access token: %s' % e
return

print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
print ' Access Token secret: %s' % resp.get('oauth_token_secret')
print ''


def main():
Expand Down

0 comments on commit a3f8ff2

Please sign in to comment.