首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >OAuthSwift (1)连接

OAuthSwift (1)连接
EN

Stack Overflow用户
提问于 2017-01-21 00:28:17
回答 1查看 848关注 0票数 0

我正在尝试创建一个客户端(在私有pod中)来连接到garmin API (OAuth1),但我在做这件事时遇到了一些问题。我使用的是OAuthSwift和OAuthSwiftAlamofire

首先我想得到所有的授权,

代码语言:javascript
复制
let oauthswift = OAuth1Swift(
        consumerKey:    "*****************",
        consumerSecret: "****************",
        requestTokenUrl: "http://connectapitest.garmin.com/oauth-service-1.0/oauth/request_token",
        authorizeUrl:    "http://connecttest.garmin.com/oauthConfirm",
        accessTokenUrl:  "http://connectapitest.garmin.com/oauth-service-1.0/oauth/access_token"
    )

oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)

let _ = oauthswift.authorize(
        withCallbackURL: URL(string: "https://www.****.co/api/v2/garminCallback")!,
        success: { credential, response, parameters in
            print("Success")
            print(credential.oauthToken)
            print(credential.oauthTokenSecret)
            print(credential.oauthVerifier)
    },
        failure: { error in
            print("Error")
            print(error.localizedDescription)
    })

AppDelegate

代码语言:javascript
复制
 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if (url.host == "oauth-callback") {
        OAuthSwift.handle(url: url)
    }
    return true
}

因此,这部分代码在safari中打开garmin的连接页面,我使用我的帐户mail/pwd进行连接,仅此而已。回调不会成功,也不会失败。所以我不能访问我的证书。它类似于授权(withCallBackURL...)不要等待callBack et永远不会获得URL中的信息(如oauth-idenfitifier)。

我不明白为什么,如果你有一个想法,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 04:35:48

我正在分享为我工作的代码

// create an instance of oAuth and retain it let oauthSwift = OAuth1Swift( consumerKey: "\*\*\*\*\*\*\*", consumerSecret: "\*\*\*\*\*\*\*", requestTokenUrl: "https://connectapi.garmin.com/oauth-service/oauth/request\_token", authorizeUrl: "https://connect.garmin.com/oauthConfirm", accessTokenUrl: "https://connectapi.garmin.com/oauth-service/oauth/access\_token" ) // add safari as authorized URL Handler oauthSwift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthSwift) // set redirection URL guard let redirectURL = URL(string: "oauth-swift://garmin-callback") else { return } // add callback url to authorized url oauthSwift.addCallbackURLToAuthorizeURL = true

代码语言:javascript
复制
      // authorized the request

oauthSwift.authorize(withCallbackURL: redirectURL, success: { (credentials, response, parameters) in print(response) }, failure: { (error) in print(error) })

//authorize call已改为如下

代码语言:javascript
复制
        oauthSwift.addCallbackURLToAuthorizeURL = true
    oauthSwift.authorize(withCallbackURL: redirectURL)  { result in
        switch result {
        case .success(let (req, response, res)):
            print("response=", response ?? "no")
            print("req=", req ?? "no")
            print("res=", res ?? "no")
            print("dataString=",response?.dataString())
            if let secrect = res["oauth_token_secret"] as? String{
                self.garminAccessTokenSecret = secrect
            }
            if let token = res["oauth_token"] as? String{
                self.garminAccessToken = token
            }
        case .failure(let error):
            print(error.description)
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41768212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档