新浪微博Python站內應用認證

23 篇文章 0 订阅
4 篇文章 0 订阅

      呢個新浪微博Python站內應用認證搞咗我兩日喇,睇咗N多Oauth2資料,寻日係度不停咁試,都試唔出個所以然,今日用python個解析器一句句試,終於搞掂。然後做到一半,微博PythonSDK嗰作者突然私信返我(我寻日問過佢),個SDK升級到1.0.9版,然後就冇然後喇... 我本來根據 http://blog.csdn.net/chenggong2dm/article/details/6613582 敲咗十幾行好核突嘅代碼,改用新SDK幾行代碼就搞掂... 以下放出基本代碼。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# handlers.py

import web
import setting
from weibo import APIClient
from jinja2 import Environment, FileSystemLoader

env = Environment(loader = FileSystemLoader('templates'))

class Redirect:
    def GET(self):
        web.found( '/' )

class Index:
    def GET(self):
        return env.get_template('goto_sina.html').render()
    def POST(self):
        # oauth2
        c = APIClient(setting._APP_KEY, setting._APP_SECRET)
        data = c.parse_signed_request( web.data().split("=")[1])
        if 'oauth_token' not in data:
            return env.get_template('unoauth.html').render()
        c.set_access_token( data['oauth_token'], data['expires_in'])

        sended = not 'rerror_code' in c.statuses.update.post(status=u'发微博')
        return sended

# index.wsgi
import sae
import web
from handlers import *

# 404 & 500
def notfound():
    return web.notfound(
            "Sorry, the page you were looking for was not found.")
def internalerror():
    return web.internalerror("Bad, bad server. No donut for you.")

# url settings
urls = (
    '/?', 'Index',
    '/.+', 'Redirect',
)

app = web.application(urls, globals())
app.notfound = notfound
#app.internalerror = internalerror
application = sae.create_wsgi_app( app.wsgifunc() )


unoauth.html
<!DOCTYPE HTML>
<html>
<head>
    <meta content="text/html; charset=gbk" http-equiv="Content-Type">
    <title>unauth</title>
    <style>
        body{
        background:url(http://css.tudouui.com/skin/2012sinaapp/img/bg_0.jpg) 50% top no-repeat #000;
        }
    </style>
    <script language="JavaScript" src="http://tjs.sjs.sinajs.cn/t35/apps/opent/js/frames/client.js"></script>
    <script language="JavaScript"> 
        function authLoad(){
            App.AuthDialog.show({
                client_id : "3802881013",
                redirect_uri : "http://apps.weibo.com/autotimer",
                height: 120
                });
        }
    </script>
</head>
<body οnlοad="authLoad();">
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值