OAuth学习--简单的web应用调用新浪微博API

在使用新浪API之前需要现在新浪微博开放平台注册一个应用,获得App key和App Secret,设置回调页。参考:http://blog.csdn.net/tom10073234/article/details/51611664。

然后使用的是廖雪峰的python sdk。https://github.com/michaelliao/sinaweibopy/blob/master/weibo.py

自己编写一个很简单的web.py。即可对这一过程熟悉一点。

#!/usr/bin/python

#-*-coding:utf8 -*-

from weibo import APIClient

import webbrowser

import pymongo

from pymongo import MongoClient

APP_KEY = '3278018605'

APP_SECRET = 'ab612e7f6b0e8531f9a8374caab1303d' #need in

CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html' #need in

client = APIClient(app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL)

url = client.get_authorize_url()

print url

webbrowser.open_new(url)

print 'input the code'

code = raw_input()

#TODO: redirect to url

#code = your.web.framework.request.get('code')

#client = APIClient(app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL)

r = client.request_access_token(code)

print  r

access_token = r.access_token

expires_in = r.expires_in

client.set_access_token(access_token,expires_in)

print client.statuses.user_timeline.get()

你可能感兴趣的:(OAuth学习--简单的web应用调用新浪微博API)