OAuthSwift(一)连接
OAuthSwift (1) connection
我正在尝试创建一个客户端(在私有 pod 中)以连接到 garmin API (OAuth1),但我遇到了一些问题。我正在使用 OAuthSwift 和 OAuthSwiftAlamofire
首先我正在尝试获得所有授权,
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
}
因此,这部分代码在safari中打开garmin的连接页面,我使用我的帐户mail/pwd进行连接,仅此而已。回调永远不会成功,或者永远不会失败。所以我无法访问我的凭据。就像 authorize(withCallBackURL...) 不要等待回调 et 永远不会在 URL 中获取信息(如 oauth-idenfitifier)。
我不明白为什么,如果你有想法谢谢。
我正在分享对我有用的代码
// 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.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)
}
}
我正在尝试创建一个客户端(在私有 pod 中)以连接到 garmin API (OAuth1),但我遇到了一些问题。我正在使用 OAuthSwift 和 OAuthSwiftAlamofire
首先我正在尝试获得所有授权,
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
}
因此,这部分代码在safari中打开garmin的连接页面,我使用我的帐户mail/pwd进行连接,仅此而已。回调永远不会成功,或者永远不会失败。所以我无法访问我的凭据。就像 authorize(withCallBackURL...) 不要等待回调 et 永远不会在 URL 中获取信息(如 oauth-idenfitifier)。
我不明白为什么,如果你有想法谢谢。
我正在分享对我有用的代码
// 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.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)
}
}