OAuthSwift (1) connection

岁酱吖の 提交于 2019-12-12 01:20:54

问题


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.


回答1:


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)
    })


来源:https://stackoverflow.com/questions/41768212/oauthswift-1-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!