拦截 oauth2 回调
Intercept oauth2 callback
我正在尝试将 iOS
Swift 应用程序连接到 API,我已经尝试了 oauthswift
、aerogear
和heimdallr
。
流程运行良好,但 API 本身没有用户拥有的资源。所有用户都可以访问所有资源。但是,API 确实需要 OAuth2 进行身份验证。
有没有办法阻止 swift 应用跳转到 Safari(或 Safariwebview)并避免用户登录部分或使用变通方法处理它?我知道这有点与 oauth2 背道而驰,但没有必要(实际上这会是一个障碍)让单个用户登录到这个 api。
基本上,我希望应用程序在后端登录以访问每个用户。我知道这个 api 在 Ruby、Python 中有 sdk,而 Node 就是这样做的。那么,我该如何在 Swift 中执行此操作?
这是我成功进入的 oauthswift 代码:
let yourEndpoint = ("https://www.awebsite.com/search/shows/a_show")
let oauthswift = OAuth2Swift(
consumerKey: "my key",
consumerSecret: "my secret",
authorizeUrl: "https://www.awebsite.com/oauth/authorize",
accessTokenUrl: "https://www.awebsite.com/oauth/token",
responseType: "token")
let name = "sample_api_proj"
oauthswift.authorizeWithCallbackURL( NSURL(string: "xxx:xxxx:xx:oauth:2.0:xxx")!, scope: "", state: "", success: {
credential, response, parameters in
self.showTokenAlert(name, credential: credential)
let parameters = Dictionary<String, AnyObject>()
oauthswift.client.get("https://www.awebsite.com/api/search/shows/ashow", parameters: parameters,
success: {
data, response in
let jsonDict: AnyObject! = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
print(jsonDict)
}, failure: { error in
print(error)
})
}, failure: { error in
print(error.localizedDescription)
})
我回到这里来提供我最终找到的答案。我没有意识到有不同的 types of oauth2, and the type used to authorize an entire app is called 'client credentials.' Not all libraries/pods are designed for this type of oauth. The working solution I found was with p2.OAuth2.
我正在尝试将 iOS
Swift 应用程序连接到 API,我已经尝试了 oauthswift
、aerogear
和heimdallr
。
流程运行良好,但 API 本身没有用户拥有的资源。所有用户都可以访问所有资源。但是,API 确实需要 OAuth2 进行身份验证。
有没有办法阻止 swift 应用跳转到 Safari(或 Safariwebview)并避免用户登录部分或使用变通方法处理它?我知道这有点与 oauth2 背道而驰,但没有必要(实际上这会是一个障碍)让单个用户登录到这个 api。
基本上,我希望应用程序在后端登录以访问每个用户。我知道这个 api 在 Ruby、Python 中有 sdk,而 Node 就是这样做的。那么,我该如何在 Swift 中执行此操作?
这是我成功进入的 oauthswift 代码:
let yourEndpoint = ("https://www.awebsite.com/search/shows/a_show")
let oauthswift = OAuth2Swift(
consumerKey: "my key",
consumerSecret: "my secret",
authorizeUrl: "https://www.awebsite.com/oauth/authorize",
accessTokenUrl: "https://www.awebsite.com/oauth/token",
responseType: "token")
let name = "sample_api_proj"
oauthswift.authorizeWithCallbackURL( NSURL(string: "xxx:xxxx:xx:oauth:2.0:xxx")!, scope: "", state: "", success: {
credential, response, parameters in
self.showTokenAlert(name, credential: credential)
let parameters = Dictionary<String, AnyObject>()
oauthswift.client.get("https://www.awebsite.com/api/search/shows/ashow", parameters: parameters,
success: {
data, response in
let jsonDict: AnyObject! = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
print(jsonDict)
}, failure: { error in
print(error)
})
}, failure: { error in
print(error.localizedDescription)
})
我回到这里来提供我最终找到的答案。我没有意识到有不同的 types of oauth2, and the type used to authorize an entire app is called 'client credentials.' Not all libraries/pods are designed for this type of oauth. The working solution I found was with p2.OAuth2.