Created
March 30, 2013 16:16
-
-
Save maxcountryman/5277290 to your computer and use it in GitHub Desktop.
Updated rauth examples supporting Python 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rauth import OAuth2Service | |
import re | |
import webbrowser | |
try: | |
read_input = raw_input | |
except NameError: | |
read_input = input | |
# Get a real client id and secret from: | |
# | |
# https://developers.facebook.com/apps | |
facebook = OAuth2Service( | |
client_id='440483442642551', | |
client_secret='cd54f1ace848fa2a7ac89a31ed9c1b61', | |
name='facebook', | |
authorize_url='https://graph.facebook.com/oauth/authorize', | |
access_token_url='https://graph.facebook.com/oauth/access_token', | |
base_url='https://graph.facebook.com/') | |
redirect_uri = 'https://www.facebook.com/connect/login_success.html' | |
params = {'scope': 'read_stream', | |
'response_type': 'token', | |
'redirect_uri': redirect_uri} | |
authorize_url = facebook.get_authorize_url(**params) | |
print('Visit this URL in your browser: {url}'.format(url=authorize_url)) | |
webbrowser.open(authorize_url) | |
url_with_token = read_input('Copy URL from your browser\'s address bar: ') | |
access_token = re.search('\#access_token=([^&]*)', url_with_token).group(1) | |
# return an authenticated session | |
session = facebook.get_session(access_token) | |
# make a request using the authenticated session | |
user = session.get('me').json() | |
print('Currently logged in as: {user}'.format(user=user['username'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rauth import OAuth2Service | |
import re | |
import webbrowser | |
try: | |
read_input = raw_input | |
except NameError: | |
read_input = input | |
# Get a real client id and secret from: | |
# | |
# https://github.com/settings/applications/new | |
github = OAuth2Service( | |
client_id='8ae4946cc5a9af76f6d7', | |
client_secret='48aeb2b3c9226ae2b698eef4d7e6310473ccafa7', | |
name='github', | |
authorize_url='https://github.com/login/oauth/authorize', | |
access_token_url='https://github.com/login/oauth/access_token', | |
base_url='https://api.github.com/') | |
authorize_url = github.get_authorize_url() | |
print('Visit this URL in your browser: {url}'.format(url=authorize_url)) | |
webbrowser.open(authorize_url) | |
url_with_code = read_input('Copy URL from your browser\'s address bar: ') | |
code = re.search('\?code=([^&]*)', url_with_code).group(1) | |
data = {'code': code, 'redirect_uri': 'https://github.com/litl/rauth/'} | |
# retrieve an access token and return authenticated session | |
session = github.get_auth_session(data=data) | |
# make a request using the authenticated session | |
user = session.get('user').json() | |
print('Currently logged in as: {user}'.format(user=user['login'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rauth import OAuth1Service | |
try: | |
read_input = raw_input | |
except NameError: | |
read_input = input | |
# Get a real consumer key and secret from: | |
# | |
# https://www.linkedin.com/secure/developer | |
linkedin = OAuth1Service( | |
consumer_key='tjm826j6uzio', | |
consumer_secret='1XbHsC7UxtC6EzqW', | |
name='linkedin', | |
request_token_url='https://api.linkedin.com/uas/oauth/requestToken', | |
authorize_url='https://api.linkedin.com/uas/oauth/authorize', | |
access_token_url='https://api.linkedin.com/uas/oauth/accessToken', | |
base_url='http://api.linkedin.com/v1/') | |
request_token, request_token_secret = linkedin.get_request_token() | |
authorize_url = linkedin.get_authorize_url(request_token) | |
print('Visit this URL in your browser: {url}'.format(url=authorize_url)) | |
pin = read_input('Enter PIN from browser: ') | |
session = linkedin.get_auth_session(request_token, | |
request_token_secret, | |
data={'oauth_verifier': pin}, | |
header_auth=True) | |
r = session.get('people/~/network/updates', | |
params={'type': 'SHAR', 'format': 'json'}, | |
header_auth=True) | |
updates = r.json() | |
for i, update in enumerate(updates['values'], 1): | |
if 'currentShare' not in update['updateContent']['person']: | |
print('{0}. {1}'.format(i, update['updateKey'])) | |
continue | |
current_share = update['updateContent']['person']['currentShare'] | |
person = current_share['author']['firstName'].encode('utf-8') + b' ' | |
person += current_share['author']['lastName'].encode('utf-8') | |
comment = current_share.get('comment', '').encode('utf-8') | |
if not comment: | |
comment = current_share['content']['description'].encode('utf-8') | |
print('{0}. {1} - {2}'.format(i, person, comment)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rauth import OAuth1Service | |
try: | |
read_input = raw_input | |
except NameError: | |
read_input = input | |
# Get a real consumer key & secret from | |
# | |
# https://dev.twitter.com/apps/new | |
twitter = OAuth1Service( | |
name='twitter', | |
consumer_key='J8MoJG4bQ9gcmGh8H7XhMg', | |
consumer_secret='7WAscbSy65GmiVOvMU5EBYn5z80fhQkcFWSLMJJu4', | |
request_token_url='https://api.twitter.com/oauth/request_token', | |
access_token_url='https://api.twitter.com/oauth/access_token', | |
authorize_url='https://api.twitter.com/oauth/authorize', | |
base_url='https://api.twitter.com/1/') | |
request_token, request_token_secret = twitter.get_request_token() | |
authorize_url = twitter.get_authorize_url(request_token) | |
print('Visit this URL in your browser: {url}'.format(url=authorize_url)) | |
pin = read_input('Enter PIN from browser: ') | |
session = twitter.get_auth_session(request_token, | |
request_token_secret, | |
method='POST', | |
data={'oauth_verifier': pin}, | |
header_auth=True) | |
params = {'include_rts': 1, # Include retweets | |
'count': 10} # 10 tweets | |
r = session.get('statuses/home_timeline.json', params=params, header_auth=True) | |
for i, tweet in enumerate(r.json(), 1): | |
handle = tweet['user']['screen_name'] | |
text = tweet['text'] | |
print('{0}. @{1} - {2}'.format(i, handle, text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
In the twitter-timeline-cli-py3.py
Python is throwing an error
Traceback (most recent call last):
File "C:/Python27/nytimes/14052014/rauth2.py", line 39, in
handle = tweet['user']['screen_name']
TypeError: string indices must be integers
Please help me to resolve it.
Regards
Sourabh