{"id":112,"date":"2018-03-10T15:06:19","date_gmt":"2018-03-10T07:06:19","guid":{"rendered":"http:\/\/www.max-shu.com\/blog\/?p=112"},"modified":"2018-03-10T15:06:19","modified_gmt":"2018-03-10T07:06:19","slug":"%e5%be%ae%e8%bd%afms%e7%9a%84live%e7%94%a8oauth%e7%bb%99%e7%ac%ac%e4%b8%89%e6%96%b9%e9%89%b4%e6%9d%83","status":"publish","type":"post","link":"http:\/\/www.max-shu.com\/blog\/?p=112","title":{"rendered":"\u5fae\u8f6fms\u7684live\u7528OAUTH\u7ed9\u7b2c\u4e09\u65b9\u9274\u6743"},"content":{"rendered":"<p>\u8fd9\u91cc\u662f\u4e2aPython\u5199\u7684\u5b9e\u4f8b\u3002<br \/>\n<span style=\"color: #0000ff;\"><b>\u7b2c\u4e00\u6b65<\/b><\/span>\u5148\u7528IE\u8bbf\u95ee\u4ee5\u4e0b\u5730\u5740\uff0c\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u3001\u5bc6\u7801\u540e\u5f97\u5230\u6821\u9a8c\u7801\u3002<br \/>\nhttps:\/\/consent.live.com\/Connect.aspx?wrap_client_id=000000004404F705&#038;wrap_callback=http:\/\/www.hanborq.com\/static\/test_ms_live_oauth.html&#038;wrap_scope=WL_Profiles.View,WL_Contacts.View<\/p>\n<p><span style=\"color: #0000ff;\"><b>\u7b2c\u4e8c\u6b65<\/b><\/span>\u7528POST\u65b9\u5f0f\u8bbf\u95ee\u4ee5\u4e0b\u5730\u5740\uff0c\u5f97\u5230access_token\u3002<br \/>\nhttps:\/\/consent.live.com\/AccessToken.aspx<br \/>\nPOST\u6570\u636e\uff1a<br \/>\n{&#8216;wrap_client_id&#8217;:&#8217;000000004404F705&#8242;, &#8216;wrap_client_secret&#8217;:&#8217;fikrtNtceeAgcvnSTo4bfA3pkUvy1J27&#8242;, &#8216;wrap_callback&#8217;:&#8217;http:\/\/www.hanborq.com\/static\/test_ms_live_oauth.html&#8217;, &#8216;wrap_verification_code&#8217;:wrap_verification_code, &#8216;idtype&#8217;:&#8217;CID&#8217;}<\/p>\n<p><span style=\"color: #0000ff;\"><b>\u7b2c\u4e09\u6b65<\/b><\/span>\u7528POST\u53c2\u6570\u65b9\u5f0f\u4f20\u5165access_token\u5f97\u5230\u7528\u6237\u5c5e\u6027JSON\u683c\u5f0f\uff0c\u542b\u7528\u6237\u540d\u3001id\u3001email\u7b49<br \/>\nhttp:\/\/apis.live.net\/V4.1\/cid-&#8216;+uid+&#8217;\/Profiles\/1-&#8216;+uid<br \/>\nreq.add_header(&#8216;Authorization&#8217;,&#8217;WRAP access_token=&#8217;+wrap_access_token)<br \/>\nreq.add_header(&#8216;Content-Type&#8217;,&#8217;application\/json&#8217;)<br \/>\nreq.add_header(&#8216;Accept&#8217;,&#8217;application\/json&#8217;)<\/p>\n<p><span style=\"color: #ff0000;\"><b>\u6e90\u6587\u4ef6\u5982\u4e0b\uff08test_live_oauth.py\uff09\uff1a<\/b><\/span><br \/>\n#!\/usr\/bin\/env python<br \/>\n# -*- coding: utf-8 -*-<\/p>\n<p>import\u00a0 os, sys, string, socket<br \/>\nimport time<br \/>\nimport urllib2<br \/>\nimport urllib<br \/>\nimport htmllib<br \/>\nimport formatter<br \/>\nimport cStringIO<br \/>\nimport traceback<br \/>\nimport datetime<br \/>\nimport codecs<br \/>\nimport win32com.client<\/p>\n<p>status = &#8216;start&#8217;<\/p>\n<p>class SmartRedirectHandler(urllib2.HTTPRedirectHandler):<br \/>\ndef http_error_301(self, req, fp, code, msg, headers): #\u6c38\u4e45\u91cd\u5b9a\u5411<br \/>\nprint &#8216;========== &#8216;+u'(301)\u6c38\u4e45\u91cd\u5b9a\u5411\uff1a&#8217;+&#8217; ==========&#8217;<br \/>\nprint str(&#8216;req=====&gt; &#8216; +req)<br \/>\nprint str(&#8216;fp======&gt; &#8216; + fp)<br \/>\nprint str(&#8216;code====&gt; &#8216; + code)<br \/>\nprint str(&#8216;msg=====&gt; &#8216;+ msg)<br \/>\nprint str(&#8216;headers=&gt; &#8216;+ headers)<br \/>\nprint &#8216;========== END ==========&#8217;<br \/>\nresult = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)<br \/>\nreturn result<\/p>\n<p>def http_error_302(self, req, fp, code, msg, headers):\u00a0 #\u4e34\u65f6\u91cd\u5b9a\u5411<br \/>\nprint &#8216;========== &#8216;+u'(302)\u4e34\u65f6\u91cd\u5b9a\u5411\uff1a&#8217;+&#8217; ==========&#8217;<br \/>\nprint str(&#8216;req=====&gt; &#8216; +req)<br \/>\nprint str(&#8216;fp======&gt; &#8216; + fp)<br \/>\nprint str(&#8216;code====&gt; &#8216; + code)<br \/>\nprint str(&#8216;msg=====&gt; &#8216;+ msg)<br \/>\nprint str(&#8216;headers=&gt; &#8216;+ headers)<br \/>\nprint &#8216;========== END ==========&#8217;<br \/>\nglobal status<br \/>\nif status == &#8216;start&#8217;:<br \/>\nstatus = &#8216;get_verify_code&#8217;<br \/>\nresult = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)<br \/>\nelse:<br \/>\nresult = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)<br \/>\nreturn result<\/p>\n<p>def DoWork():<br \/>\ntry:<br \/>\n# \u5f97\u5230\u6821\u9a8c\u7801verification_code<br \/>\nreq = &#8216;https:\/\/consent.live.com\/Connect.aspx?wrap_client_id=000000004404F705&amp;wrap_callback=http:\/\/www.hanborq.com\/static\/test_ms_live_oauth.html&amp;wrap_scope=WL_Profiles.View,WL_Contacts.View&#8217;<br \/>\nie=win32com.client.Dispatch(&#8220;InternetExplorer.Application&#8221;)<br \/>\nie.Navigate(req)<br \/>\nie.Visible=1<br \/>\nie.Toolbar = 0<br \/>\nie.StatusBar=0<br \/>\nie.AddressBar=0<br \/>\nie.menubar=0<br \/>\nie.FullScreen=0<br \/>\nie.Resizable=1<br \/>\n#ie.left=-5;<br \/>\n#ie.top=-25;<br \/>\nie.Height=480;<br \/>\nie.Width=640;<br \/>\nwhile ie.Busy:<br \/>\ntime.sleep(1)<br \/>\n# \u7b49\u5f85\u7a97\u53e3\u5927\u5c0f\u6539\u53d8\uff08\u9700\u8981\u7b49\u5f85\u9274\u6743\u5b8c\u6210\uff0cr\u7136\u540e\u624b\u5de5\u6539\u53d8\uff09\uff0c\u6765\u68c0\u6d4b\u662f\u5426\u5b8c\u6210\u9274\u6743\u3002<br \/>\nwhile ie.Width == 640:<br \/>\ntime.sleep(1)<br \/>\nprint &#8216;URL===&gt; &#8216;+ie.LocationURL<br \/>\nprint &#8216;========== &#8216;+u&#8217;\u5185\u5bb9\uff1a&#8217;+&#8217; ==========&#8217;<br \/>\nprint ie.Document<br \/>\nprint &#8216;========== END ==========&#8217;<\/p>\n<p>urlContent = ie.LocationURL<br \/>\nurlContent = urlContent.partition(&#8216;?&#8217;)[2]<br \/>\nif urlContent == &#8221;:<br \/>\nprint &#8216;ERROR: ie.LocationURL is Null&#8217;<br \/>\nie.Stop()<br \/>\nie.Quit()<br \/>\nie = &#8221;<br \/>\nreturn<br \/>\npars = urlContent.split(&#8216;&amp;&#8217;)<br \/>\nwrap_verification_code = &#8221;<br \/>\nexp = &#8221;<br \/>\nfor par in pars:<br \/>\nname = par.partition(&#8216;=&#8217;)[0]<br \/>\nvalue = par.partition(&#8216;=&#8217;)[2]<br \/>\nif name == &#8216;wrap_verification_code&#8217;:<br \/>\nwrap_verification_code = value<br \/>\nelif name == &#8216;exp&#8217;:<br \/>\nexp = value;<br \/>\nelse:<br \/>\nprint &#8216;URL PARAMETERS: NAME: &#8216; + name + &#8216;, VALUE: &#8216; + value<br \/>\nprint &#8216;wrap_verification_code=&gt; &#8216; + wrap_verification_code<br \/>\nprint &#8216;exp====================&gt; &#8216; + exp<br \/>\nif wrap_verification_code == &#8221;:<br \/>\nprint &#8216;ERROR: wrap_verification_code is Null&#8217;<br \/>\nie.Stop()<br \/>\nie.Quit()<br \/>\nie = &#8221;<br \/>\nreturn<br \/>\nif exp == &#8221;:<br \/>\nprint &#8216;ERROR: exp is Null&#8217;<br \/>\nie.Stop()<br \/>\nie.Quit()<br \/>\nie = &#8221;<br \/>\nreturn<br \/>\nie.Stop()<br \/>\nie.Quit()<br \/>\nie = &#8221;<br \/>\ntime.sleep(1)<\/p>\n<p># \u5f97\u5230access_token<br \/>\nsech = urllib.urlencode({&#8216;wrap_client_id&#8217;:&#8217;000000004404F705&#8242;, &#8216;wrap_client_secret&#8217;:&#8217;fikrtNtceeAgcvnSTo4bfA3pkUvy1J27&#8242;, &#8216;wrap_callback&#8217;:&#8217;http:\/\/www.hanborq.com\/static\/test_ms_live_oauth.html&#8217;, &#8216;wrap_verification_code&#8217;:wrap_verification_code, &#8216;idtype&#8217;:&#8217;CID&#8217;})<br \/>\nreq = urllib2.Request(&#8216;https:\/\/consent.live.com\/AccessToken.aspx&#8217;)<br \/>\nopener = urllib2.build_opener(SmartRedirectHandler())<br \/>\nfd = opener.open(req, sech) #urllib2.urlopen(req,sech)<br \/>\nurl = fd.url<br \/>\nwebContent = fd.read()<br \/>\nprint &#8216;URL===&gt; &#8216; + url<br \/>\nprint &#8216;========== &#8216;+u&#8217;\u5185\u5bb9\uff1a&#8217;+&#8217; ==========&#8217;<br \/>\nprint webContent<br \/>\nprint &#8216;========== END ==========&#8217;<br \/>\nif url == &#8221;:<br \/>\nprint &#8216;ERROR: fd.url is Null&#8217;<br \/>\nreturn<\/p>\n<p>pars = webContent.split(&#8216;&amp;&#8217;)<br \/>\nwrap_access_token = &#8221;<br \/>\nwrap_access_token_expires_in = &#8221;<br \/>\nwrap_refresh_token = &#8221;<br \/>\nskey = &#8221;<br \/>\nuid = &#8221;<br \/>\nfor par in pars:<br \/>\nname = par.partition(&#8216;=&#8217;)[0]<br \/>\nvalue = par.partition(&#8216;=&#8217;)[2]<br \/>\nif name == &#8216;wrap_access_token&#8217;:<br \/>\nwrap_access_token = value<br \/>\nelif name == &#8216;wrap_access_token_expires_in&#8217;:<br \/>\nwrap_access_token_expires_in = value;<br \/>\nelif name == &#8216;wrap_refresh_token&#8217;:<br \/>\nwrap_refresh_token = value;<br \/>\nelif name == &#8216;skey&#8217;:<br \/>\nskey = value;<br \/>\nelif name == &#8216;uid&#8217;:<br \/>\nuid = value;<br \/>\nelse:<br \/>\nprint &#8216;URL PARAMETERS: NAME: &#8216; + name + &#8216;, VALUE: &#8216; + value<br \/>\nprint &#8216;wrap_access_token============&gt; &#8216; + wrap_access_token<br \/>\nprint &#8216;wrap_access_token_expires_in=&gt; &#8216; + wrap_access_token_expires_in<br \/>\nprint &#8216;wrap_refresh_token===========&gt; &#8216; + wrap_refresh_token<br \/>\nprint &#8216;skey=========================&gt; &#8216; + skey<br \/>\nprint &#8216;uid==========================&gt; &#8216; + uid<br \/>\nif wrap_access_token == &#8221;:<br \/>\nprint &#8216;ERROR: wrap_access_token is Null&#8217;<br \/>\nreturn<br \/>\nif wrap_access_token_expires_in == &#8221;:<br \/>\nprint &#8216;ERROR: wrap_access_token_expires_in is Null&#8217;<br \/>\nreturn<br \/>\nif wrap_refresh_token == &#8221;:<br \/>\nprint &#8216;ERROR: wrap_refresh_token is Null&#8217;<br \/>\nreturn<br \/>\nif skey == &#8221;:<br \/>\nprint &#8216;ERROR: skey is Null&#8217;<br \/>\nreturn<br \/>\nif uid == &#8221;:<br \/>\nprint &#8216;ERROR: uid is Null&#8217;<br \/>\nreturn<br \/>\ntime.sleep(1)<\/p>\n<p># \u5f97\u5230\u7528\u6237\u5c5e\u6027<br \/>\nsech = None<br \/>\nreq = urllib2.Request(&#8216;http:\/\/apis.live.net\/V4.1\/cid-&#8216;+uid+&#8217;\/Profiles\/1-&#8216;+uid)<br \/>\nreq.add_header(&#8216;Authorization&#8217;,&#8217;WRAP access_token=&#8217;+wrap_access_token)<br \/>\nreq.add_header(&#8216;Content-Type&#8217;,&#8217;application\/json&#8217;)<br \/>\nreq.add_header(&#8216;Accept&#8217;,&#8217;application\/json&#8217;)<br \/>\nopener = urllib2.build_opener(SmartRedirectHandler())<br \/>\nfd = opener.open(req, sech) #urllib2.urlopen(req,sech)<br \/>\nurl = fd.url<br \/>\nwebContent = fd.read()<br \/>\nprint &#8216;URL===&gt; &#8216; + url<br \/>\nprint &#8216;========== &#8216;+u&#8217;\u5185\u5bb9\uff1a&#8217;+&#8217; ==========&#8217;<br \/>\nprint webContent.decode(&#8216;utf-8&#8217;)<br \/>\nprint &#8216;========== END ==========&#8217;<br \/>\nif url == &#8221;:<br \/>\nprint &#8216;ERROR: fd.url is Null&#8217;<br \/>\nreturn<\/p>\n<p>cid = webContent.partition(&#8216;&#8221;Cid&#8221;:&#8221;&#8216;)[2]<br \/>\ncid = cid.partition(&#8216;&#8221;,&#8217;)[0]<br \/>\nfirst_name = webContent.partition(&#8216;&#8221;FirstName&#8221;:&#8221;&#8216;)[2]<br \/>\nfirst_name = first_name.partition(&#8216;&#8221;,&#8217;)[0]<br \/>\nlast_name = webContent.partition(&#8216;&#8221;LastName&#8221;:&#8221;&#8216;)[2]<br \/>\nlast_name = last_name.partition(&#8216;&#8221;,&#8217;)[0]<br \/>\nemail = webContent.partition(&#8216;&#8221;Emails&#8221;:[&#8216;)[2]<br \/>\nemail = email.partition(&#8216;],&#8217;)[0]<br \/>\nprint &#8216;CID ==========&gt; &#8216; +\u00a0 cid.decode(&#8216;utf-8&#8217;)<br \/>\nprint &#8216;FIRST NAME ===&gt; &#8216; +\u00a0 first_name.decode(&#8216;utf-8&#8217;)<br \/>\nprint &#8216;LAST NAME ====&gt; &#8216; +\u00a0 last_name.decode(&#8216;utf-8&#8217;)<br \/>\nprint &#8216;EMAIL ========&gt; &#8216; +\u00a0 email.decode(&#8216;utf-8&#8217;)<br \/>\nprint &#8216;CHEERS, Result is OK!&#8217;<br \/>\nexcept urllib2.URLError, e:<br \/>\nprint traceback.format_exc()<br \/>\nprint str(e)<br \/>\nreturn<br \/>\nexcept Exception,e:<br \/>\nprint traceback.format_exc()<br \/>\nprint str(e)<br \/>\nreturn<\/p>\n<p>if __name__ == &#8216;__main__&#8217; :<br \/>\nDoWork()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u91cc\u662f\u4e2aPython\u5199\u7684\u5b9e\u4f8b\u3002 \u7b2c\u4e00\u6b65\u5148\u7528IE\u8bbf\u95ee\u4ee5\u4e0b\u5730\u5740\uff0c\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u3001\u5bc6\u7801\u540e\u5f97\u5230\u6821\u9a8c\u7801\u3002 https:\/ &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[72,78,75],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-4","tag-oauth","tag-78","tag-75"],"views":1663,"_links":{"self":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":1,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":113,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/113"}],"wp:attachment":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}