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()

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容