OAuth? ???? ??? ??? ???? ??? ?3? ??????? ????? ??? ???? ?? ???(?: ??? ?? ??, ??, ???, ??? ??)? ???? ???? ???? ?? ? ??? ?? ??? ???????. ? ?? ??????? ?? ????. ? ????? OAuth ????? WeChat? ???? ??? ??? ?????.
OAuth2.0 ???? ??
OAuth2.0? OAuth ????? ?? ????? OAuth 1.0? ?? ???? ????. OAuth 2.0? ????? ???? ?? ???? ??? ?? ??? ? ??????, ???? ??????, ??? ? ?? ??? ??? ?? ??? ?????. OAuth 2.0? ???? ???? ??? ??? ???? ?? ??? ???? ?? ??? ????? ??? ???? ???? ? ????. ? ??? ?? ????(?: ??? ?? ????)? ?? ??(?: ?? 2?? ??) ?? ?? ???(?: ?? ??? ????)? ???? ? ??? ?????. ??? ???? OAuth? ???? ???? ??? ???? ???? ?? ??? ???? ??? ?3? ????? ?? ??? ???? ??? ??? ???? ? ??? ??? ? ????.
OAuth2.0 ?? ? ?? ??? ???? ????:
Oauth2.0 ?? ? ?? ?? ????? ???? ? ???? ??? ????.
1. ???? ??, ???, ??? ??? ?? ??? ???? ???? ? ???? ??? ???.
2. ??? ???? ??? ?? ???? ???? ??????.
3. ?????, ??? ????? ???(????? ?? ?? ???? ???? ???? ?)? ?????? ?? ?????????. ?? ????? ?? ?????? ??? ?????? ????? ID? ???? ???.
?? ? ??? ?? OAuth? ???? ??? ??? ????.
1. ???? ?????? ????? ???? ???? ??? ???? ???? ????? ???. ???;
2. ?????? ??? ????? ?? ??? ?????.
3. ??? ???? ?????? ??? ??? ? ?? ??? ?????.
4. ?????? ?? ??? ??? ? ??? ??? ???? ?? ??? ???? ?? ???? ???? ?????. ? ???? ?? ??? ?????? ?? ??? ??? ????? ?????.
5. ???? ??? ???? ? ???? ??? ??? ????? ??? ?? ?????? ??? ??? ???? ? ??? ?????. ;
6. ??? ??? ? ??? ???? ????? ?????? ? ???? ????? ?????.
7.
8. ??? ???? ?? ??? ???? ??? ???? ?????? ??? ??? ?????.
?????? ??? ???? ?????. ??? ???? ???? ??? ?????.
WeChat ???? OAuth2.0 ??:
???? WeChat ?? ??? ?3? ????? ???? ??(Web WeChat ??), ?? ?? ???? ?? ?? ??? ??(???, ??, ??, ?? ??). ??? ??? ???? ?? ???, ??? ?? ??, ?? ???, ??? ?? ?? ?? ??? ??? ? ????.
??? ?? ?? ??? ?? ?????(?? ??? ????? ?? ??)? ???? ?? ?????? ?? ??? ? ???? OpenID? ???? ???? ?? ??? ?? ? ??? ?? ???? ???. ???? ?? ??? ??? ?? ??? ???? ??? ???? ?? ??? ? ???? ???? ??? ??? ???? ?????? ?????. , ?? ??? ?? ? ????. (? ????? ???? ?? ??? ???? ??? ????.)
????? ???? ?? ???? ??? ?? ??? ?? ??????.
?? ?? ??? ?? ??:
WeChat ?? ??? ??? ???? ??? ???? ?? ???? ??? ?????. ?? ?? ??? ????? ? ??? ????? ?? ?? ??? ?????. ??? ??? ???? http:// ?? https://? ???? ????. ?? ?? ?? ??? ?? ?? ??? ?? ??? ?????. ?? ?? ? ??? ??? ??? ??? ??? www.qq.com???. ?? ??? http:/? ?? ??? ??? ?????. /www.qq.com/music.html, http: //www.qq.com/login.html? OAuth2.0 ??? ??? ? ????. ?, http://pay.qq.com ? http://music.qq.com??? OAuth2.0 ??? ??? ? ????.
?? ?? ??? ???? ???(?? ??? ?? ?? ?? ?? ??? ???? ? ???? ?? ?????. ??? ??? ?? ?????? ?? ???? ? ????.) OAuth2.0 ???? ?? ???? ?? ?? ??? ?????:
?? ?? ??? ??? ???? ?? ??? ???? ?????:
???? ??? ???? ??? ????:
? ??? ?? ??? OAuth2.0 ?? ????? ? ?? ??? "?????? ??? ????? ?? ??? ?????"? ?????. ? ??? ?? WeChat? OAuth2? ??? ? ????. .0 ?????? ???? ??? ???? ? ?????? ????? ?? ??? URL? ???? ???. ?? ???? Force.com? ????? ??? ??? ??? ???. ????? ???? ????? ??? ??? https://ap1.salesforce.com???. ???? ?? ???? https://ap1.salesforce.com/apex/oauth2test? ???? Force.com?? ???? ???? ???? ???? ?????. "Create Page oauth2test" ??? ?? ???? ?????:
??? ??? ????? ???? ??? ???? ??????. . ?? ??? ?? ???? ??? ??? ?? ?? ?? ????. ????? Force.com? ?? ??? ???? ?? ?? ??, ?? ?? ? CSS ???? ?????. ??>
? ?? ?? ?? ??? ???? Force?? .com? ?? CSS ???? ???? ?? ?? ? ?? ?? ??? ???? ??? ??? ???? ??? ?? ???? ???? ?????. aspx ???? ???? aspx.cs ???? ??? apex ???? ???? ???? ?? ?? ?? ?? ??? Ctrl + S ? ??? ?? ??? ??? ?????.
??? ?????. "Create Apex class 'public class oauth2testcontroller'"?? ? ?? ??? ???? ???? ???? ?????. ? ? ?? ??? ??? ???? "with Sharing" ??????. ? ???? ?? ???? ?? ???? ???? ??? ??? ??(??? ???? ??), ?? ?? ?? ??? ??? ??? ?????. ???? ??? Apex ????? ?? ??? ??? ???? ? ????. ?? ??? ?? ?? ?? ??? ?? ???? ??? ?? ????.
? ???? ?? ??? ?????.
public class oauth2testcontroller { public String code {get; set;} public oauth2testcontroller(){ code = ApexPages.currentPage().getParameters().get('code'); if(String.isBlank(code)){ code = 'No Code'; } } }
? ??? ? ?? ?? ?? ?? ??? ?????. ? ?? ?? ApexPages ??? ?? URL? ?? ????? ??? ?? ?? ?? ?? ??? ?????. ?? ????? ???? ?????. ????? WeChat ??? ????? ? URL? ???? ?? ????? URL? ???? ?? ? ? ????.
<apex:page standardstylesheets="false" showHeader="false" sidebar="false" controller="oauth2testcontroller"> {!code} </apex:page>{!Object name}? Force.com?? ?? ?? ???? ? ???? ?????. Visualforce ???, ???? ?? ????? ?? ???? ? ??? ???? ???? ???. ??? ??? Force.com? ????? ?? –> ???? ???? ?? ??? ???? ?????.
?? ?? ???? ??? ? "Site Visualforce ???"? ?? ???? ?? ??? ?????.
?? ???? oauth2test ???? ?? ?????. ????? ???? ?? ??? ?????.
?? ???? ? ????. http://johnson0001-developer-edition.ap1.force.com/oauth2test ?? ???? ??? ?? ??? ??? ??????. ???? WeChat ???? OAuth2 ?? ?????? ???? URL? ???? ???? WeChat? ?? ?????? ??? ? ????.
若提示“該鏈接無法訪問”,請檢查參數(shù)是否填寫錯(cuò)誤,是否擁有scope參數(shù)對應(yīng)的授權(quán)作用域權(quán)限,其中每個(gè)參數(shù)的詳細(xì)說明如下:
在我們的例子里URL如下,其中scope我們指定為snsapi_userinfo,彈出授權(quán)頁面:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3b3aef2c09447269&redirect_uri=http://johnson0001-developer-edition.ap1.force.com/oauth2test&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
興許是測試賬號的關(guān)系,雖然微信接口文檔里提到在制定scope為snsapi_userinfo的情況下會(huì)彈出如下圖左所示的授權(quán)頁面,但反復(fù)嘗試(乃至刪除并重新關(guān)注賬號)中也沒有看到該頁面,不過重點(diǎn)是我們得到了臨時(shí)令牌,如下圖右所示。右圖實(shí)際是http://johnson0001-developer-edition.ap1.force.com/oauth2test頁面,用戶同意授權(quán)后跳轉(zhuǎn)到(或者我遇到的實(shí)際情況是直接跳轉(zhuǎn))到redirect_uri/?CODE&state=STATE。若用戶禁止授權(quán),則重定向后不會(huì)帶上code參數(shù),僅會(huì)帶上state參數(shù)redirect_uri?state=STATE。
另外特別需要說明的是,code作為換取access_token的臨時(shí)票據(jù),每次用戶授權(quán)帶上的code都不一樣,code只能使用一次,5分鐘未被使用自動(dòng)過期。
通過Code換取網(wǎng)頁授權(quán)access_token:
首先請注意,這里通過code換取的網(wǎng)頁授權(quán)access_token,與基礎(chǔ)支持中的access_token不同。公眾號可通過下述接口來獲取網(wǎng)頁授權(quán)access_token。如果網(wǎng)頁授權(quán)的作用域?yàn)閟nsapi_base,則本步驟中獲取到網(wǎng)頁授權(quán)access_token的同時(shí),也獲取到了 openid,snsapi_base式的網(wǎng)頁授權(quán)流程即到此為止。 獲取code后,可以通過以下接口獲取access_token:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
這里的CODE即為通過前面方式獲得的臨時(shí)令牌(票據(jù)),參數(shù)的具體說明如下:
將URL直接輸入到瀏覽器地址欄即可得到返回?cái)?shù)據(jù),當(dāng)然真實(shí)場景里更多通過后臺代碼來請求,正確返回時(shí)的JSON數(shù)據(jù)包如下:
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
參數(shù)的具體說明如下:
錯(cuò)誤時(shí)微信會(huì)返回JSON數(shù)據(jù)包如下(示例為Code無效錯(cuò)誤):
{"errcode":40029,"errmsg":"invalid code"}
在本例中獲得的access_token實(shí)例如下:
{"access_token":"OezXcEiiBSKSxW0eoylIeMEUA_AZuBDY8AO0IIw270MMsvemqLvgx1HqemeXIZfzXW2d6yHCPy9cA1yHZ1jHCkwlH5Ct5Jfa1jOQm88M9LzU_O8BCKMNhN7yLlHJfOFLuf4lLTNGOOsoWYxQzYVNGw","expires_in":7200,"refresh_token":"OezXcEiiBSKSxW0eoylIeMEUA_AZuBDY8AO0IIw270MMsvemqLvgx1HqemeXIZfz_Vj5pJZlv2V5wK9EzWmxQmM07cqIAwMXOdqzlQs-NY4hiyENP4WhO4Twpko-3iY_pAPZRnGGmAVt3DirZaWIyg","openid":"ou-37t936RNZEcW0mI75RN2pdxkc","scope":"snsapi_userinfo"}
可以看到上面access_token的默認(rèn)失效時(shí)間是7200秒,也就是2小時(shí),當(dāng)access_token超時(shí)后,可以通過refresh_token進(jìn)行刷新,refresh_token擁有較長的有效期(7天、30天、60天、90天),當(dāng)refresh_token失效后,需要用戶重新授權(quán),簡化理解起見,我們在本文的最后再介紹相關(guān)技術(shù)。
拉取用戶信息(需Scope為snasapi_userinfo):
通過access_token獲取用戶信息的接口如下,使用GET方法:
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
參數(shù)具體說明如下:
本例的URL如下:
https://api.weixin.qq.com/sns/userinfo?access_token=OezXcEiiBSKSxW0eoylIeMEUA_AZuBDY8AO0IIw270MMsvemqLvgx1HqemeXIZfzXW2d6yHCPy9cA1yHZ1jHCkwlH5Ct5Jfa1jOQm88M9LzU_O8BCKMNhN7yLlHJfOFLuf4lLTNGOOsoWYxQzYVNGw&openid=ou-37t936RNZEcW0mI75RN2pdxkc&lang=zh_CN
輸入瀏覽器訪問即可得到相應(yīng)的用戶信息:
{"openid":"ou-37t936RNZEcW0mI75RN2pdxkc","nickname":"王浩","sex":1,"language":"zh_CN","city":"松江","province":"上海","country":"中國","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/lqsZNvDqcXe8nBKHBPsp9YHuZXPtkzOD1uq3r3xxDicuDLKGlicNd1b371ODnn9xNBB9y9ChBSfL7tuX6m9FS8koY9Ex1iaJRDI\/0","privilege":[]}
刷新access_token:
可以通過前面在“通過Code換取網(wǎng)頁授權(quán)access_token”小節(jié)中獲得的refresh_token來調(diào)用刷新Token接口獲取更新的access_token,微信在API文檔里介紹refresh_token擁有較長的有效期(7天、30天、60天、90天),但實(shí)際微信的refresh_token的有效期是多長沒有具體說明,如果有具體經(jīng)驗(yàn)的朋友歡迎分享。微信刷新access_token的接口如下:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
接口的具體參數(shù)定義如下:
正確時(shí)返回的JSON數(shù)據(jù)包如下:
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
數(shù)據(jù)包的具體定義如下:
錯(cuò)誤時(shí)微信會(huì)返回JSON數(shù)據(jù)包如下(示例為Code無效錯(cuò)誤):
{"errcode":40029,"errmsg":"invalid code"}
更多Force.com WeChat ?? ??? OAuth2.0 ? ??相關(guān)文章請關(guān)注PHP中文網(wǎng)!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)