首頁 >後端開發 >Python教學 >tornado登陸豆瓣程式碼實例

tornado登陸豆瓣程式碼實例

高洛峰
高洛峰原創
2016-10-17 14:33:181279瀏覽

分享一段tornado登陸豆瓣的程式碼,只簡單實現了登陸,並沒有實現非同步登陸。需要的網友可以自己將程式碼完善一下。

直接上碼:

#coding=utf8
import settings
import urllib
from tornado import httpclient
import json
  
class douban:
    authurl = 'https://www.douban.com/service/auth2/'
    user_info_url = 'https://api.douban.com/v2/user/~me'
  
    def get_authorization_code(self):
        params = {
            "client_id":settings.oauth2['douban']['key'],
            "redirect_uri":settings.oauth2['redirect_url'],
            "response_type":"code",
            "scope":"douban_basic_common",
        }
        return self.authurl+'auth?'+urllib.urlencode(params)
  
    def get_access_token(self,code):
        params = {
            "client_id":settings.oauth2['douban']['key'],
            "client_secret":settings.oauth2['douban']['sercet'],
            "redirect_uri":settings.oauth2['redirect_url'],
            "grant_type":"authorization_code",
            "code":code,
        }
        url = self.authurl+'token'
        http_client = httpclient.HTTPClient()
        req = httpclient.HTTPRequest(url,method='POST',body=urllib.urlencode(params))
        response = http_client.fetch(req)
        return json.loads(response.body)
              
    def get_user_info(self,access_token):
        url = 'https://api.douban.com/v2/user/~me'
        http_client = httpclient.HTTPClient()
        req = httpclient.HTTPRequest(url,headers={"Authorization":"Bearer "+access_token})
        response = http_client.fetch(req)
        return json.loads(response.body)


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn