【问题标题】:Swift OAuth2 p2Swift OAuth2 p2
【发布时间】:2017-05-04 01:23:55
【问题描述】:

一段时间以来,我一直在尝试让 oauth2 与 Untappd api (https://untappd.com/api/docs#authentication) 一起工作,并且在使用一些不同的 oauth2 框架时遇到了死胡同。

我一直在尝试 P2 OAuth。我似乎能够启动身份验证过程,用户登录,我得到一个代码并在将该代码发送回 untappd 以获取令牌时遇到问题。我已经脱离了 P2 oauth 中的示例:

class ViewController: UIViewController {

fileprivate var alamofireManager: SessionManager?

var loader: OAuth2DataLoader?

var oauth2 = OAuth2CodeGrantNoTokenType(settings: [
    "client_id": "A0******************",
    "client_secret": "BA******************",
    "authorize_uri": "https://untappd.com/oauth/authenticate",
    "token_uri": "https://untappd.com/oauth/authorize",   
    "redirect_uris": ["****://oauthcallback"], 
    "response_type": "code",
    "secret_in_body": false,
    "keychain": true,
] as OAuth2JSON)

@IBOutlet var imageView: UIImageView?
@IBOutlet var signInEmbeddedButton: UIButton?
@IBOutlet var signInSafariButton: UIButton?
@IBOutlet var signInAutoButton: UIButton?
@IBOutlet var forgetButton: UIButton?


@IBAction func signInEmbedded(_ sender: UIButton?) {
    if oauth2.isAuthorizing {
        oauth2.abortAuthorization()
        return
    }

    sender?.setTitle("Authorizing...", for: UIControlState.normal)

    oauth2.authConfig.authorizeEmbedded = true
    oauth2.authConfig.authorizeContext = self
    oauth2.verbose = true

    oauth2.authorize() { authParameters, error in
        if let params = authParameters {
            print("Authorized! Access token is in `oauth2.accessToken`")
            print("Authorized! Additional parameters: \(params)")
        }
        else {
            print("Authorization was cancelled or went wrong: \(error)")   // error will not be nil
        }
    }

}

在日志中我似乎找回了代码,但最终交换失败:

[调试] OAuth2:在https://untappd.com/oauth/authorize 交换代码 2010D2*********** 以获取访问令牌 重定向

[调试] OAuth2:是否交换代码以访问 [false] 并刷新 [false] 令牌 授权!访问令牌位于oauth2.accessToken 授权!附加参数:[“元”:{ "error_detail" = "缺少 client_id、redirect_url、client_secret 或 code 参数。请检查您的请求再试一次。"; "error_type" = "param_error"; “http_code”=500; }, "响应": <__nsarray0>( ) ]

OAuth2 和 Swift 3 似乎没有太大帮助,除非我找错地方了。有任何想法吗?

【问题讨论】:

    标签: swift oauth-2.0


    【解决方案1】:

    想通了。以防其他人遇到这种情况。原来只需要修改 OAuth2CodeGrant 类以添加在代码交换期间未传递的 client_secret:

    open func accessTokenRequest(with code: String, params: OAuth2StringDict? = nil) throws -> OAuth2AuthRequest {
        guard let clientId = clientConfig.clientId, !clientId.isEmpty else {
            throw OAuth2Error.noClientId
        }
        guard let redirect = context.redirectURL else {
            throw OAuth2Error.noRedirectURL
        }
    
        guard let clientSecret = clientConfig.clientSecret else {
            throw OAuth2Error.noClientSecret
        }
    
        let req = OAuth2AuthRequest(url: (clientConfig.tokenURL ?? clientConfig.authorizeURL), method: .GET)
    
        req.params["code"] = code
        req.params["grant_type"] = type(of: self).grantType
        req.params["redirect_uri"] = redirect
        req.params["client_id"] = clientId
        req.params["client_secret"] = clientSecret
    
        return req
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多