


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、_網(wǎng)頁端知識要點1. 網(wǎng)頁資料npm庫阮一峰的OAuth2 OAuth2.0的 碼模式 main&id=mp1421140842 2 0.html碼模式(authorization code)是功能最完整、流程最嚴密的 模式。它的特點就是通過客戶端的后臺服務(wù)器,與服務(wù)提供商的認證服務(wù)器進行互動。(A) 用戶 客戶端,后者將前者導(dǎo)向認證服務(wù)器。(B) 用戶選擇是否給予客戶端 。(C) 假設(shè)用戶給予 ,認證服務(wù)器將用戶導(dǎo)向客戶端事先指定的重定向URI(redirection URI),同時附上一個 碼。(D) 客戶端收到 碼,附上早先的重定向URI,向認證服務(wù)器申請令牌。這一步是在客戶端的 的服務(wù)
2、器上完成的,對用戶不可見。開課吧web全棧架構(gòu)師(E)認證服務(wù)器核對了 碼和重定向URI,確認無誤后,向客戶端 令牌(access token)和更新令牌(refresh token)。獲取用戶信息 - 相當(dāng)于普通網(wǎng)頁的用戶登錄生成認證URL跳轉(zhuǎn) 認證服務(wù)回調(diào)服務(wù)端 獲取code調(diào)用 接口 AccessToken與OpenId調(diào)用服務(wù)器端接口開課吧web全棧架構(gòu)師1. 配置網(wǎng)頁回調(diào)2. 配置JS安全接口1 網(wǎng)頁端網(wǎng)頁 登錄獲取用戶信息/ wechat/index.html async auth () window.location.href = /wxAuthorize,1. 初始化Oauth
3、開課吧web全棧架構(gòu)師/ index.jsconst OAuth = require(co-wechat-oauth);const oauth = new OAuth(conf.appid, conf.appsecret);2. 生成用戶URL/ index.js/ 生成引導(dǎo)用戶點擊的 URLrouter.get(/wxAuthorize, async (ctx, next) = const state = ctx.query.id/ const target = ctx.hrefconsole.log(ctx. + ctx.href)/ 目標地址redirectUrl = ctx.hrefr
4、edirectUrl = redirectUrl.replace(wxAuthorize, wxCallback) const scope = snsapi_userinfovar url = oauth.getAuthorizeURL(redirectUrl, state, scope); console.log(url + url)ctx.redirect(url)3. 獲取用戶回調(diào) AccessToken與OpenId/ index.js/ 獲取AccessTokenrouter.get(/wxCallback, async (ctx, next) = const code = ctx.
5、query.code / 碼console.log(getAccessToken, code)var token = await oauth.getAccessToken(code); var accessToken = token.data.access_token; var openid = token.data.openid; console.log(getAccessToken.)console.log(accessToken, accessToken) console.log(openid, openid)/ ctx.body = tokenctx.redirect(/?openid
6、= + openid)4. 用戶信息開課吧web全棧架構(gòu)師/ index.html async getUser()const qs = Qs.parse(window.location.search.substr(1) const openid = qs.openidconst res = await axios.get(/getUser, params:openid)console.log(User,res.data),/ index.jsrouter.get(/getUser, async (ctx, next) = const openid = ctx.query.openid cons
7、ole.log(getUser, openid)var userInfo = await oauth.getUser(openid); console.log(userInfo:, userInfo) ctx.body = userInfo)5. AccessToken緩存問題開課吧web全棧架構(gòu)師/ mongo.js/ ClientAccessToken/ mongoose.jsconst mongoose = require(mongoose) const Schema = mongoosemongoose.connect(mongodb:/localhost:27017/weixin,
8、useNewUrlParser: true, () = console.log(Mongodb connected.)exports.ServerToken = mongoose.m (ServerToken, accessToken: String);/ ClientAccessToken schema = new Schema(access_token: String, expires_in: Number, refresh_token: String, openid: String,scope: String, create_at: String);const OAuth = requi
9、re(co-wechat-oauth);const oauth = new OAuth(conf.appid, conf.appsecret, async function (openid) return await ClientToken.getToken(openid),async function (openid, token) return await ClientToken.setToken(openid, token)2. JSSDK資料:npm庫:是開發(fā)者在網(wǎng)頁上通過JavaScript代碼使用 main&id=mp1421141115(獲取JSCong)原生功能的工具包,開發(fā)者
10、可以使用它在網(wǎng)頁上錄制和 微信語音、上傳 本地圖片、拍照等許多能力運行于 內(nèi)置瀏覽器的網(wǎng)頁調(diào)用 原生應(yīng)用如:拍照、語音、掃一掃功能 查到的數(shù)據(jù)不同圖像接口音頻接口開課吧web全棧架構(gòu)師/ 自定義getToken方法schema.statics.getToken = async function (openid) return await this.findOne(openid: openid);schema.statics.setToken = async function (openid, token) / 有則更新,無則添加const query = openid: openid;cons
11、t options = upsert: true;return await this.updateOne(query, token, options);exports.ClientToken = mongoose.m (ClientToken, schema);/ index.js/ 獲取JSConfig router.get(/getJsConfig, async ctx = console.log(getJSSDK., ctx.query)var res = await api.getJsConfig(ctx.query); console.log(res, res)ctx.body = res)/ index.html/ index.htmlgetJSConfig: async function () console.log(wx:, wx)let res = await axios.get(/getJSConfig, params: url: window.location.href)console.log(res., res.data)res.data.jsApiList = on ShareTimeline, on ShareAppMessage wx.config(res.data);wx.ready(function () console.log(wx.read
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025公司主要負責(zé)人安全培訓(xùn)考試試題加答案可下載
- 2025年公司廠級員工安全培訓(xùn)考試試題及完整答案【名校卷】
- 2025年廠級安全培訓(xùn)考試試題附完整答案(各地真題)
- 2025年浙江省事業(yè)單位勞動合同書范本
- 2025職場維權(quán):大學(xué)生就業(yè)招聘需審視合同細節(jié)
- 2025標準酒店員工合同范本
- 2025短期勞務(wù)合同模板
- 2025年溶栓藥項目合作計劃書
- 2025房屋租賃合同樣本下載
- 2025年度網(wǎng)站廣告服務(wù)合同協(xié)議范本
- 琴行老師勞動協(xié)議合同
- 2025-2030中國干燥劑行業(yè)發(fā)展分析及發(fā)展前景與投資研究報告
- 新疆維吾爾自治區(qū)和田地區(qū)2024-2025學(xué)年高三5月考試題語文試題試卷含解析
- 環(huán)保安全知識課件
- 比例尺單元測試卷及答案
- 氬弧焊基本知識課件
- 《廣西壯族自治區(qū)基層工會經(jīng)費收支管理實施辦法》修訂解讀
- 2024北京朝陽城市發(fā)展集團有限公司社會化招聘專場筆試參考題庫附帶答案詳解
- 中職語文教學(xué)大賽教學(xué)實施報告范文與解析
- 山東臨沂市羅莊區(qū)興羅投資控股有限公司招聘筆試題庫2025
- 北京市朝陽區(qū)2025屆高三下學(xué)期一模試題 數(shù)學(xué) 含答案
評論
0/150
提交評論