前言
之前讓網頁公司製作新官網的時候規劃有第三方帳號
的登入功能,但由於當時的一些開放平台申請步驟比較繁瑣(尤其是微信開放平台),所以一直拖延著,到了最近只能自己加入相關的功能。
由於是剛接觸<a href="http://www.php.cn/wiki/1514.html" target="_blank">Python</a>
和<a href="http://www.php.cn/wiki/1515.html" target="_blank">Django</a>
,我找了好多影片和資料學習練習,才慢慢把MVT結構
什麼的弄清楚了,第三方登入方面百度找到了兩篇很有用的文章,也從中學習到了很多:
1. python實作微信第三方網站掃碼登入(Django)
2. 用django-social-auth 做中國社群網站三方登入(QQ,微博,豆瓣,百度,人人,微信)
在實現QQ、微博的登入時就深刻體會到使用social-auth
來實現第三方登入是非常簡單方便、直接而且完美的,然而卻一直沒有找到微信
怎麼也同樣來實現(上面第二篇文章也一樣沒有提及);從social-auth
的解讀文件裡也愣是沒找到Weixin的內容,由於官網已經有相應的User
資料表以及儲存第三方的UserSocialAuth
資料格,非常規範,在使用上面第一種方法實作之後卻苦惱於使用者資料表的新增和修改,真心不想破壞那種結構,就在重拾social-auth
想學習一下資料庫儲存方式的時候,竟然在social-back<a href="http://www.php.cn/wiki/1048.html" target="_blank">end</a>s
裡發現了Weixin.py
,那不就說明能支持微信麼?
注意事項
#微信開放平台
申請及開通需要提交許多認證資料,也需要繳交¥300每年的認證費用;跟公眾號、服務號等不一樣哦。網址:http://open.weixin.qq.com認證通過後,加入對應的網頁應用,注意
授權回調域
的填寫,寫網站的主網域即可,比如說不能寫www.zzmxy.com/login/wechat
之類的,只需要寫www.zzmxy.com
即可(不需要加入http或https),不然後期都是re<a href="http://www.php.cn/wiki/1275.html" target="_blank">dir</a>ect_uri 參數錯誤
!
##實操步驟
-
安裝social-auth
:
由於官網使用的是
python-social-auth==0.2.12,下載原始碼回來之後,發現在
social-backends裡也是有
Weixin.py的,證明可用;
pip install python-social-auth==0.2.12
social-auth
設定:
SOCIAL_AUTH_PIPELINE
設定:請參考上述的第二篇文章的書寫;
-
AUTHENTICATION_BACKENDS
設定:
AUTHENTICATION_BACKENDS = ( 'social.backends.weibo.WeiboOAuth2', #微博的功能 'social.backends.qq.QQOAuth2', #QQ的功能 'social.backends.weixin.WeixinOAuth2', #这个是导入微信的功能 'oscar.apps.customer.auth_backends.EmailBackend', 'django.contrib.auth.backends.ModelBackend', )
- 微信開放平台應用程式
APPID
與
SECRET的配置:
SOCIAL_AUTH_WEIBO_KEY = '53*****29' SOCIAL_AUTH_WEIBO_SECRET = '272152************81a8b3' SOCIAL_AUTH_QQ_KEY = '10*****51' SOCIAL_AUTH_QQ_SECRET = '5807************d15bd97' SOCIAL_AUTH_WEIXIN_KEY = 'wx4fb***********599' #开放平台应用的APPID SOCIAL_AUTH_WEIXIN_SECRET = 'f1c17************08c0489' #开放平台应用的SECRET
Scope參數錯誤或沒有Scope權限,實際操作過程中,我發現
social-auth自動產生的二維碼存取連結裡,是少了一個
scope參數,而微信官方給的二維碼存取連結是這樣的:
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect如上需要的參數為5個,
state可省略,但
scope 則是必需的,而對於網頁授權的訪問,
scope作用域參數為固定值
scope=snsapi<a href="http://www.php.cn/js/js-weixinapp-api.html" target="_blank">_login</a>,這樣的話,我們就需要在
social-auth的原始程式碼上,把這個參數值給加上,根據你實際的
site-packages安裝路徑,找到
/social/backends/ weixin.py文件,如我使用
VirtualEnv建造的路徑是:
/home/ubuntu/env/mppython/lib/python2.7/site-packages/social/backends/weixin.py打開這個文件,找到裡面
def auth_params()這一段內容(原文):
def auth_params(self, state=None): appid, secret = self.get_key_and_secret() params = { 'appid': appid, 'redirect_uri': self.get_redirect_uri(state), } if self.STATE_PARAMETER and state: params['state'] = state if self.RESPONSE_TYPE: params['response_type'] = self.RESPONSE_TYPE return params在
params字典裡,加入一個
scope參數即可,修改後如下:
def auth_params(self, state=None): appid, secret = self.get_key_and_secret() params = { 'appid': appid, 'redirect_uri': self.get_redirect_uri(state), 'scope': 'snsapi_login', } if self.STATE_PARAMETER and state: params['state'] = state if self.RESPONSE_TYPE: params['response_type'] = self.RESPONSE_TYPE return params修改儲存後,再重新運行工程,再次訪問www.域名.com/login/weixin 即可看到效果了!
#
以上是Django使用Social-Auth實作微信第三方網站掃碼登入的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器