OAuthSwift (1) 连接 [英] OAuthSwift (1) connection

查看:97
本文介绍了OAuthSwift (1) 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个客户端(在一个私有 pod 中)来连接到 garmin API (OAuth1),但我有一些问题要做.我正在使用 OAuthSwift 和 OAuthSwiftAlamofire

首先,我试图获得所有授权,

let oauthswift = OAuth1Swift(消费者密钥:********************",消费者秘密:****************",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)让 _ = oauthswift.authorize(withCallbackURL: URL(string: "https://www.****.co/api/v2/garminCallback")!,成功:{ 凭据,响应,参数打印(成功")打印(凭据.oauthToken)打印(credential.oauthTokenSecret)打印(credential.oauthVerifier)},失败:{ 错误在打印(错误")打印(错误.本地化描述)})

AppDelegate:

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) ->布尔{if (url.host == "oauth-callback") {OAuthSwift.handle(url: url)}返回真}

所以,这部分代码在safari中打开garmin的连接页面,我用我的帐户mail/pwd连接,仅此而已.回调永远不会成功,或者永远不会失败.所以我无法访问我的凭据.这就像 authorize(withCallBackURL...) 不要等待 callBack 永远不会获取 URL 中的信息(如 oauth-idenfitifier).

我不明白为什么,如果你有想法,谢谢.

解决方案

我正在分享对我有用的代码

<块引用>

//创建一个 oAuth 实例并保留它让 oauthSwift = OAuth1Swift(消费者密钥:*******",消费者秘密:********",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")//添加 safari 作为授权的 URL HandleroauthSwift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthSwift)//设置重定向地址守卫 let redirectURL = URL(string: "oauth-swift://garmin-callback") else { return }//添加回调url到授权urloauthSwift.addCallbackURLToAuthorizeURL = true

//授权请求

<块引用>

 oauthSwift.authorize(withCallbackURL: redirectURL, success: { (credentials, response, parameters) in打印(响应)}, 失败: { (error) in打印(错误)})

I'm trying to create a client (in a private pod) to connect to garmin API (OAuth1) and i've some problem to do it. I'm using OAuthSwift and OAuthSwiftAlamofire

First i'm trying to get all the authorization,

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:

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

So, this part of code open the connection page of garmin in safari, i use my account mail/pwd to connect and that's all. The callback never sucess, or never fail. So i can't access to my credentials. It's like authorize(withCallBackURL...) don't wait the callBack et never get the information in the URL (like oauth-idenfitifier).

I'dont understand why, if you have an idea thank's.

解决方案

I'm Sharing my Code that's working for me

    // 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

      // authorized the request

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

这篇关于OAuthSwift (1) 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆