0

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.

Makaille
  • 1,636
  • 2
  • 24
  • 41
  • I am having a problem connecting to Garmin. Where and how did you add Signature Method and timestamp? – abhi Feb 20 '17 at 16:52
  • @abhi Are you using OAuthSwift ? – Makaille Feb 21 '17 at 16:32
  • Yes, Makaille. I have the similar code as yours which you mentioned above. But unable to connect to Garmin. Can you please share your code. – abhi Feb 21 '17 at 16:50
  • TimeStamp and Signature methode are set by ouathswift client. Make sure you've correctly config all the client (appDelegate, and target build). I'm not using this anymore i can't share you code – Makaille Feb 21 '17 at 16:59

1 Answers1

0

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)
        }
    }
Waseem Sarwar
  • 2,645
  • 1
  • 21
  • 18