Python指令碼實現自動髮帶圖的微博

來源:互聯網
上載者:User
要自動發微博最簡單的辦法無非是調用新浪微博的API(因為只是簡單的發微博,就沒必要用它的SDK了)。參考開發文檔http://open.weibo.com/wiki/API 進行代碼編寫

建立應用

要使用微博的API,需先要有個應用。隨便是個應用就行,可以到這裡註冊一個站內應用應用註冊。註冊應用的主要目的是要獲得MY_APPKEY 和MY_ACCESS_TOKEN,

擷取access_token

API的調用需要登入授權獲得access_token。參考

首先,調用https://api.weibo.com/oauth2/authorize介面,獲得code。

該介面有三個必須的參數:

•client_id:申請應用時分配的AppKey。
•redirect_url:就是建立應用中設定的回調地址
•response_type:響應類型,可設定為code

具體做法,就是在瀏覽器開啟https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code。該方法會轉到授權頁面,授權之後會轉到http://www.example.com/response&code=CODE,記錄下該url中的CODE。

接著,調用https://api.weibo.com/oauth2/access_token介面,獲得access_token。

該介面有如下必須的參數:

•client_id:申請應用時分配的AppKey。
•client_secret:申請應用時分配的AppSecret。
•grant_type:請求的類型,填寫authorization_code
•code:調用authorize獲得的code值。
•redirect_uri: 就是建立應用中設定的回調地址

具體做法就是構建一個POST請求,再在返回的資料中找到access_token,儲存下來。具體的Python代碼:

import requestsurl_get_token = "https://api.weibo.com/oauth2/access_token"#構建POST參數playload = {"client_id":"填入你的","client_secret":"填入你的","grant_type":"authorization_code","code":"上面獲得的CODE","redirect_uri":"你的回調用地址"}#POST請求r = requests.post(url_get_token,data=playload)#輸出響應資訊print r.text 

如果正常的話,會返回下面這樣的json資料:

{"access_token":"我們要記下的","remind_in":"157679999","expires_in":157679999,"uid":"1739207845"}

根據返回的資料,access_token的值就是我們要的。其中remind_in的值是access_token的有效期間,單位為秒,我們可以看到,這個時間有3、4年之久,足夠我們用了。

發表純文字微博

調用介面https://api.weibo.com/2/statuses/update.json發表文字微博,其參數如下

其中必須的:

•access_token: 就是我們上一步獲得的access_token
•status:要發布的微博常值內容,必須做URLencode,內容不超過140個漢字

具體代碼:

#發表文字微博的介面url_post_a_text = "https://api.weibo.com/2/statuses/update.json"#構建POST參數playload = {"access_token":"填入你的","status":"This is a text test@TaceyWong"}#POST請求,發表文字微博r = requests.post(url_post_a_text,data = playload) 

如果正常,會有向下面這樣的結果

發錶帶圖片的微博

調用介面http://open.weibo.com/wiki/2/statuses/upload發表圖片微博,其參數如下:

其中必須的參數:

•access_token: 就是我們上一步獲得的access_token
•status:要發布的微博常值內容,必須做URLencode,內容不超過140個漢字
•pic:要發表的圖片,採用multipart/form-data編碼方式

具體的代碼:

#發表圖文微博的介面url_post_pic = "https://upload.api.weibo.com/2/statuses/upload.json"#構建文本類POST參數playload={"access_token":"2.0086XhtBIQLH7Ed67706b6c8TQ8XdE","status":"Test:Post a text with a pic & AT someone@丸子覠"}#構建二進位multipart/form-data編碼的參數files={"pic":open("logo.png","rb")}#POST請求,發表微博r = requests.post(url_post_pic,data=playload,files = files)

如果正常,結果會像下面這樣:

註:requests的具體用法請參考[requests文檔](http://docs.python-requests.org/en/master/)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.