您好,登錄后才能下訂單哦!
這篇文章給大家介紹iOSMoya中怎么實(shí)現(xiàn)OAuth請(qǐng)求,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
1. 環(huán)境
項(xiàng)目使用 MVVM 架構(gòu),引入了 Rx 全家桶,網(wǎng)絡(luò)請(qǐng)求框架使用了 Moya,以及處理 Oauth 相關(guān)的庫(kù) OAuth3。
2. OAuth3 部分
參閱 OAuth3 庫(kù)的 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)
同時(shí)因?yàn)?Moya 的底層網(wǎng)絡(luò)請(qǐng)求實(shí)現(xiàn)是基于 Alamofire,因此我們可以參照 OAuth3 提供的說(shuō)明文檔 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 在初始化時(shí)可以傳入 SessionManager ,因此參照文檔,可以先使用 OAuth3 生成一個(gè)特定的 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 }
進(jìn)而生成帶 OAuth 的 provider:
fileprivate lazy var provider: MoyaProvider = { return MoyaProvider<API>(manager: self.getManager(), plugins: [NetworkLoggerPlugin()])}()
關(guān)于iOSMoya中怎么實(shí)現(xiàn)OAuth請(qǐng)求就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。