這篇文章給大家介紹iOSMoya中怎么實現(xiàn)OAuth請求,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)專注于網(wǎng)站設(shè)計制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴謹?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。
1. 環(huán)境
項目使用 MVVM 架構(gòu),引入了 Rx 全家桶,網(wǎng)絡(luò)請求框架使用了 Moya,以及處理 Oauth 相關(guān)的庫 OAuth3。
2. OAuth3 部分
參閱 OAuth3 庫的 README,完成 OAuth 的信息配置:
let oauth3 = OAuth3CodeGrant(settings: [ "client_id": "my_swift_app", "client_secret": "C7447242", "authorize_uri": "https://github.com/login/oauth/authorize", "token_uri": "https://github.com/login/oauth/access_token", "redirect_uris": ["myapp://oauth/callback"], "scope": "user repo:status", "secret_in_body": true, "keychain": false,] as OAuth3JSON)
同時因為 Moya 的底層網(wǎng)絡(luò)請求實現(xiàn)是基于 Alamofire,因此我們可以參照 OAuth3 提供的說明文檔 Alamofire 4 · p2/OAuth3 Wiki · GitHub完成相關(guān)配置,關(guān)鍵代碼如下:
import Foundationimport OAuth3import Alamofireclass OAuth3RetryHandler: RequestRetrier, RequestAdapter { let loader: OAuth3DataLoader init(oauth3: OAuth3) { loader = OAuth3DataLoader(oauth3: oauth3) } /// Intercept 401 and do an OAuth3 authorization. public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) { if let response = request.task?.response as? HTTPURLResponse, 401 == response.statusCode, let req = request.request { var dataRequest = OAuth3DataRequest(request: req, callback: { _ in }) dataRequest.context = completion loader.enqueue(request: dataRequest) loader.attemptToAuthorize() { authParams, error in self.loader.dequeueAndApply() { req in if let comp = req.context as? RequestRetryCompletion { comp(nil != authParams, 0.0) } } } } else { completion(false, 0.0) // not a 401, not our problem } } /// Sign the request with the access token. public func adapt(_ urlRequest: URLRequest) throws -> URLRequest { guard nil != loader.oauth3.accessToken else { return urlRequest } return try urlRequest.signed(with: loader.oauth3) // "try" added in 3.0.2 }}
3. Moya 部分
Moya 的 provider 在初始化時可以傳入 SessionManager ,因此參照文檔,可以先使用 OAuth3 生成一個特定的 SessionManager :
func getManager() -> SessionManager { let oauth3 = OAuth3CodeGrant(settings: [ "client_id": "my_swift_app", "client_secret": "C7447242", "authorize_uri": "https://github.com/login/oauth/authorize", "token_uri": "https://github.com/login/oauth/access_token", "redirect_uris": ["myapp://oauth/callback"], "scope": "user repo:status", "secret_in_body": true, "keychain": false, ] as OAuth3JSON) let sessionManager = SessionManager() let oauthHandler = OAuth3Handler(oauth3: oauth3) sessionManager.adapter = oauthHandler sessionManager.retrier = oauthHandler return sessionManager }
進而生成帶 OAuth 的 provider:
fileprivate lazy var provider: MoyaProvider = { return MoyaProvider<API>(manager: self.getManager(), plugins: [NetworkLoggerPlugin()])}()
關(guān)于iOSMoya中怎么實現(xiàn)OAuth請求就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
名稱欄目:iOSMoya中怎么實現(xiàn)OAuth請求
文章網(wǎng)址:http://www.rwnh.cn/article28/jeshcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、App設(shè)計、Google、網(wǎng)站維護、企業(yè)建站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)