Skip to content

Instantly share code, notes, and snippets.

@mrluanma
Created March 7, 2012 02:08
Show Gist options
  • Select an option

  • Save mrluanma/1990456 to your computer and use it in GitHub Desktop.

Select an option

Save mrluanma/1990456 to your computer and use it in GitHub Desktop.
Sina weibo OAuth 1.0a example using requests-oauth https://github.com/maraujop/requests-oauth
# -*- encoding: utf-8 -*-
import os
import urlparse
import requests
from oauth_hook import OAuthHook
REQUEST_TOKEN_URL = 'http://api.t.sina.com.cn/oauth/request_token'
ACCESS_TOKEN_URL = 'http://api.t.sina.com.cn/oauth/access_token'
AUTHORIZE_URL = 'http://api.t.sina.com.cn/oauth/authorize'
STATUSES_UPDATE_URL = 'https://api.t.sina.com.cn/statuses/update.json'
STATUSES_UPLOAD_URL = 'https://api.t.sina.com.cn/statuses/upload.json'
OAuthHook.consumer_key = 'XXXXXXXXX'
OAuthHook.consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX'
oauth_hook = OAuthHook(header_auth=True)
client = requests.session(hooks={'pre_request': oauth_hook})
# Step 1: Get a request token. This is a temporary token that is used for
# having the user authorize an access token and to sign the request to obtain
# said access token.
resp = client.get(REQUEST_TOKEN_URL)
if resp.status_code != 200:
raise Exception("Invalid resp %s." % resp.status_code)
request_token = dict(urlparse.parse_qsl(resp.content))
print "Token: %s Secret: %s" % (request_token['oauth_token'], request_token['oauth_token_secret'])
oauth_hook = OAuthHook(request_token['oauth_token'], request_token['oauth_token_secret'], header_auth=True)
client = requests.session(hooks={'pre_request': oauth_hook})
# Step 2: Redirect to the provider. Since this is a CLI script we do not
# redirect. In a web application you would redirect the user to the URL
# below.
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (AUTHORIZE_URL, request_token['oauth_token'])
print
# After the user has granted access to you, the consumer, the provider will
# redirect you to whatever URL you have told them to redirect to. You can
# usually define this in the oauth_callback argument as well.
accepted = 'n'
while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ')
oauth_verifier = raw_input('What is the PIN? ')
# Step 3: Once the consumer has redirected the user back to the oauth_callback
# URL you can request the access token the user has approved. You use the
# request token to sign this request. After this is done you throw away the
# request token and use the access token returned. You should store this
# access token somewhere safe, like a database, for future use.
resp = client.post(ACCESS_TOKEN_URL, {'oauth_verifier': oauth_verifier})
if resp.status_code != 200:
raise Exception("Invalid resp %s." % resp.status_code)
access_token = dict(urlparse.parse_qsl(resp.content))
print "Token: %s Secret: %s" % (access_token['oauth_token'], access_token['oauth_token_secret'])
print
print "You may now access protected resources using the access tokens above."
print
oauth_hook = OAuthHook(access_token['oauth_token'], access_token['oauth_token_secret'], header_auth=True)
client = requests.session(hooks={'pre_request': oauth_hook})
def update_status(status, lat=None, lng=None, pic_url=None):
data = dict(status=status)
if lat:
data['lat'] = lat
if lng:
data['long'] = lng
if pic_url is None:
resp = client.post(STATUSES_UPDATE_URL, data)
else:
pic_filename = os.path.basename(urlparse.urlparse(pic_url).path)
files = {'pic': (pic_filename, requests.get(pic_url).raw)}
resp = client.post(STATUSES_UPLOAD_URL, data, files=files)
return resp
if __name__ == '__main__':
print update_status(u'杀代码').content
print update_status(u'带图片杀代码', pic_url='http://img2.codoon.com/dgfP9RAe7h_1.png').content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment