牛骨文教育服务平台(让学习变的简单)

Python:新浪微博应用开发简介(认证及授权部分)

  很早之前写了两篇通过python程序发送新浪微博的文章(《Python:通过命令行发送新浪微博》《Python:通过摄像头抓取图像并自动上传至新浪微博》),刚看到有朋友邮件咨询其中有关细节问题,感到文章没有写清楚,故新写一篇,补充开发中的一些细节。

一、注册个新浪微博账号,方法略。

二、在开放平台上注册个应用,网址:http://open.weibo.com/

三、注册后会得到应用的相关信息,包括App Key和App Secret,如下是我注册的一个应用的相关信息:

应用名称:auto_press
应用类型:普通应用  -  客户端
App Key:230143xxxx
App Secret:0ada3bf3feab026050df37d7xxxxxxxx
创建时间:2012-02-04
应用平台:桌面  -  Windows
标签:同步工具  发布帮助  
应用介绍:一款通过PC同时向多个不同的微博发送信息的客户端软件,提高信息推送效率。

四、下载weibo开发SDK(python版本),网址:http://code.google.com/p/sinatpy/downloads/list

五、编码、根据步骤三申请的App Key和App Secret得到token和token sercret。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from weibopy.auth import OAuthHandler
from weibopy.api import API
   
def get_sina_token():    
    """
    Function:获取token相关信息
    Input:NONE
    Output: NONE
    author: socrates
    blog:http://blog.csdn.net/dyx1024
    date:2012-04-05
    """  
    
    #申请应用时得到的App Key及密码
    App_key = "230143xxxx"
    App_secret = "0ada3bf3feab026050df37d7xxxxxxxx"

    #授权
    auth_handler = OAuthHandler(App_key, App_secret)
    auth_url = auth_handler.get_authorization_url()

    print "Please open this url by your Browser:" + auth_url
    verifier = raw_input("Please input PIN code get from above url: ").strip()
    
    #得到token及密码
    auth_handler.get_access_token(verifier)

if __name__ == "__main__":
    get_sina_token()

简介:1、运行上面的程序(注,将App_key和App_secret的值换成你自己的哦,上面的我仅举例,你直接复制是执行不成功的);  

Please open this url by your Browser:http://api.t.sina.com.cn/oauth/authorize?oauth_token=df33909872943783a75b6ab274abac3d
Please input PIN code get from above url: 

2、运行后会得到一个网址,用浏览器打开这个网址,会得到PIN码的值,如下:          

3、将得到的PIN码输入到命令行中,得到token及密码,如下:

Please open this url by your Browser:http://api.t.sina.com.cn/oauth/authorize?oauth_token=b09bdbcb398a50ce2358fab823c291ec
Please input PIN code get from above url: 715505
Access token key: 2a21b19910af7a4b1962ad6efdb6xxxx
Access token secret: 47e2fdb0b0ac983241b0caaf457cxxxx

六、参考《Python:通过命令行发送新浪微博》开发一个最简单的应用。