iosswiftoauthoauth-1.0a

OAuthSwift (1) connection


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.


Solution

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

    //authorize call has been changed to below

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