Created
February 20, 2014 02:47
-
-
Save Kevinstronger/9106185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import BaseHTTPServer | |
from BaseHTTPServer import BaseHTTPRequestHandler | |
from urlparse import parse_qs | |
import httplib, urllib | |
import json | |
PARAMS_STRING = "?redirect_uri=http://127.0.0.1&client_secret=[SECRET]&client_id=[CLIENT_ID]&grant_type=authorization_code&code=" | |
class Handler(BaseHTTPRequestHandler): | |
def do_GET(s): | |
s.do_HEAD() | |
def do_HEAD(s): | |
# get request token | |
s.send_response(200) | |
params = parse_qs(s.path[2:]) | |
# use request token to get access token | |
conn = httplib.HTTPSConnection("api.diandian.com") | |
p = PARAMS_STRING + params['code'][0] | |
conn.request("GET", "/oauth/token" + p ) | |
response = conn.getresponse() | |
data = response.read() | |
data_map = json.loads(data) | |
uid = data_map['uid'] | |
access_token = data_map['access_token'] | |
print "uid = ", uid | |
print "access_token = ", access_token | |
# use uid and access token to get blog information | |
conn = httplib.HTTPSConnection("api.diandian.com") | |
addr = "/v1/blog/" + "iam.diandian.com" + "/info?access_token=" + access_token | |
print addr | |
conn.request("GET", addr) | |
response = conn.getresponse() | |
data = response.read() | |
data_map = json.loads(data) | |
print data_map | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
s.wfile.write("<html><head><title>Blog Information</title></head><body>") | |
s.wfile.write("<p>title: %s</p>" % data_map['response']['title']) | |
s.wfile.write("<p>name: %s</p>" % data_map['response']['name']) | |
s.wfile.write("</body></html>") | |
HandlerClass = Handler | |
ServerClass = BaseHTTPServer.HTTPServer | |
Protocol = "HTTP/1.0" | |
if sys.argv[1:]: | |
port = int(sys.argv[1]) | |
else: | |
port = 80 | |
server_address = ('127.0.0.1', port) | |
HandlerClass.protocol_version = Protocol | |
httpd = ServerClass(server_address, HandlerClass) | |
sa = httpd.socket.getsockname() | |
print "Serving HTTP on", sa[0], "port", sa[1], "..." | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment