微软ms的live用OAUTH给第三方鉴权
这里是个Python写的实例。
第一步先用IE访问以下地址,用户输入用户名、密码后得到校验码。
https://consent.live.com/Connect.aspx?wrap_client_id=000000004404F705&wrap_callback=http://www.hanborq.com/static/test_ms_live_oauth.html&wrap_scope=WL_Profiles.View,WL_Contacts.View
第二步用POST方式访问以下地址,得到access_token。
https://consent.live.com/AccessToken.aspx
POST数据:
{‘wrap_client_id’:’000000004404F705′, ‘wrap_client_secret’:’fikrtNtceeAgcvnSTo4bfA3pkUvy1J27′, ‘wrap_callback’:’http://www.hanborq.com/static/test_ms_live_oauth.html’, ‘wrap_verification_code’:wrap_verification_code, ‘idtype’:’CID’}
第三步用POST参数方式传入access_token得到用户属性JSON格式,含用户名、id、email等
http://apis.live.net/V4.1/cid-‘+uid+’/Profiles/1-‘+uid
req.add_header(‘Authorization’,’WRAP access_token=’+wrap_access_token)
req.add_header(‘Content-Type’,’application/json’)
req.add_header(‘Accept’,’application/json’)
源文件如下(test_live_oauth.py):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, string, socket
import time
import urllib2
import urllib
import htmllib
import formatter
import cStringIO
import traceback
import datetime
import codecs
import win32com.client
status = ‘start’
class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_301(self, req, fp, code, msg, headers): #永久重定向
print ‘========== ‘+u'(301)永久重定向:’+’ ==========’
print str(‘req=====> ‘ +req)
print str(‘fp======> ‘ + fp)
print str(‘code====> ‘ + code)
print str(‘msg=====> ‘+ msg)
print str(‘headers=> ‘+ headers)
print ‘========== END ==========’
result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
return result
def http_error_302(self, req, fp, code, msg, headers): #临时重定向
print ‘========== ‘+u'(302)临时重定向:’+’ ==========’
print str(‘req=====> ‘ +req)
print str(‘fp======> ‘ + fp)
print str(‘code====> ‘ + code)
print str(‘msg=====> ‘+ msg)
print str(‘headers=> ‘+ headers)
print ‘========== END ==========’
global status
if status == ‘start’:
status = ‘get_verify_code’
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
else:
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
return result
def DoWork():
try:
# 得到校验码verification_code
req = ‘https://consent.live.com/Connect.aspx?wrap_client_id=000000004404F705&wrap_callback=http://www.hanborq.com/static/test_ms_live_oauth.html&wrap_scope=WL_Profiles.View,WL_Contacts.View’
ie=win32com.client.Dispatch(“InternetExplorer.Application”)
ie.Navigate(req)
ie.Visible=1
ie.Toolbar = 0
ie.StatusBar=0
ie.AddressBar=0
ie.menubar=0
ie.FullScreen=0
ie.Resizable=1
#ie.left=-5;
#ie.top=-25;
ie.Height=480;
ie.Width=640;
while ie.Busy:
time.sleep(1)
# 等待窗口大小改变(需要等待鉴权完成,r然后手工改变),来检测是否完成鉴权。
while ie.Width == 640:
time.sleep(1)
print ‘URL===> ‘+ie.LocationURL
print ‘========== ‘+u’内容:’+’ ==========’
print ie.Document
print ‘========== END ==========’
urlContent = ie.LocationURL
urlContent = urlContent.partition(‘?’)[2]
if urlContent == ”:
print ‘ERROR: ie.LocationURL is Null’
ie.Stop()
ie.Quit()
ie = ”
return
pars = urlContent.split(‘&’)
wrap_verification_code = ”
exp = ”
for par in pars:
name = par.partition(‘=’)[0]
value = par.partition(‘=’)[2]
if name == ‘wrap_verification_code’:
wrap_verification_code = value
elif name == ‘exp’:
exp = value;
else:
print ‘URL PARAMETERS: NAME: ‘ + name + ‘, VALUE: ‘ + value
print ‘wrap_verification_code=> ‘ + wrap_verification_code
print ‘exp====================> ‘ + exp
if wrap_verification_code == ”:
print ‘ERROR: wrap_verification_code is Null’
ie.Stop()
ie.Quit()
ie = ”
return
if exp == ”:
print ‘ERROR: exp is Null’
ie.Stop()
ie.Quit()
ie = ”
return
ie.Stop()
ie.Quit()
ie = ”
time.sleep(1)
# 得到access_token
sech = urllib.urlencode({‘wrap_client_id’:’000000004404F705′, ‘wrap_client_secret’:’fikrtNtceeAgcvnSTo4bfA3pkUvy1J27′, ‘wrap_callback’:’http://www.hanborq.com/static/test_ms_live_oauth.html’, ‘wrap_verification_code’:wrap_verification_code, ‘idtype’:’CID’})
req = urllib2.Request(‘https://consent.live.com/AccessToken.aspx’)
opener = urllib2.build_opener(SmartRedirectHandler())
fd = opener.open(req, sech) #urllib2.urlopen(req,sech)
url = fd.url
webContent = fd.read()
print ‘URL===> ‘ + url
print ‘========== ‘+u’内容:’+’ ==========’
print webContent
print ‘========== END ==========’
if url == ”:
print ‘ERROR: fd.url is Null’
return
pars = webContent.split(‘&’)
wrap_access_token = ”
wrap_access_token_expires_in = ”
wrap_refresh_token = ”
skey = ”
uid = ”
for par in pars:
name = par.partition(‘=’)[0]
value = par.partition(‘=’)[2]
if name == ‘wrap_access_token’:
wrap_access_token = value
elif name == ‘wrap_access_token_expires_in’:
wrap_access_token_expires_in = value;
elif name == ‘wrap_refresh_token’:
wrap_refresh_token = value;
elif name == ‘skey’:
skey = value;
elif name == ‘uid’:
uid = value;
else:
print ‘URL PARAMETERS: NAME: ‘ + name + ‘, VALUE: ‘ + value
print ‘wrap_access_token============> ‘ + wrap_access_token
print ‘wrap_access_token_expires_in=> ‘ + wrap_access_token_expires_in
print ‘wrap_refresh_token===========> ‘ + wrap_refresh_token
print ‘skey=========================> ‘ + skey
print ‘uid==========================> ‘ + uid
if wrap_access_token == ”:
print ‘ERROR: wrap_access_token is Null’
return
if wrap_access_token_expires_in == ”:
print ‘ERROR: wrap_access_token_expires_in is Null’
return
if wrap_refresh_token == ”:
print ‘ERROR: wrap_refresh_token is Null’
return
if skey == ”:
print ‘ERROR: skey is Null’
return
if uid == ”:
print ‘ERROR: uid is Null’
return
time.sleep(1)
# 得到用户属性
sech = None
req = urllib2.Request(‘http://apis.live.net/V4.1/cid-‘+uid+’/Profiles/1-‘+uid)
req.add_header(‘Authorization’,’WRAP access_token=’+wrap_access_token)
req.add_header(‘Content-Type’,’application/json’)
req.add_header(‘Accept’,’application/json’)
opener = urllib2.build_opener(SmartRedirectHandler())
fd = opener.open(req, sech) #urllib2.urlopen(req,sech)
url = fd.url
webContent = fd.read()
print ‘URL===> ‘ + url
print ‘========== ‘+u’内容:’+’ ==========’
print webContent.decode(‘utf-8’)
print ‘========== END ==========’
if url == ”:
print ‘ERROR: fd.url is Null’
return
cid = webContent.partition(‘”Cid”:”‘)[2]
cid = cid.partition(‘”,’)[0]
first_name = webContent.partition(‘”FirstName”:”‘)[2]
first_name = first_name.partition(‘”,’)[0]
last_name = webContent.partition(‘”LastName”:”‘)[2]
last_name = last_name.partition(‘”,’)[0]
email = webContent.partition(‘”Emails”:[‘)[2]
email = email.partition(‘],’)[0]
print ‘CID ==========> ‘ + cid.decode(‘utf-8’)
print ‘FIRST NAME ===> ‘ + first_name.decode(‘utf-8’)
print ‘LAST NAME ====> ‘ + last_name.decode(‘utf-8’)
print ‘EMAIL ========> ‘ + email.decode(‘utf-8’)
print ‘CHEERS, Result is OK!’
except urllib2.URLError, e:
print traceback.format_exc()
print str(e)
return
except Exception,e:
print traceback.format_exc()
print str(e)
return
if __name__ == ‘__main__’ :
DoWork()