'OAuth2: How to do Authentication with Unsplash API
I'm writing an iOS app to using Unsplash API. And I get below issue when dealing with authentication workflow(login via website):
Following Authorization workflow on Unsplash API, I could get authorization code on the redirect page.
1) What I opened: https://unsplash.com/oauth/authorize?client_id={myclientid}&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=public
2) Safari get to the response page with Authorization code
3) Then I need to make a POST request to https://unsplash.com/oauth/token and include the Authorization code
/ / / My question is how to get the Authorization code in Step 2 with Swift?, I could deal with JSON response, but this is a page view in return, what class or method I should use to retrieve the code value?
/ / / Well, here is my code
class FancyClient {
static let clientID = "clientID"
static let clientSceret = "clientSceret"
struct Auth {
static var accessToken = ""
static var authorizationCode = ""
}
enum Endpoints {
static let base = "https://unsplash.com"
static let apiKeyParam = "?client_id=\(FancyClient.clientID)"
static let apiSecretParam = "?client_secret=\(FancyClient.clientSceret)"
case getAuthorizationCode
case login
case getAccessToken
var stringValue: String {
switch self {
case .getAuthorizationCode:
return Endpoints.base + "/oauth/authorize" + Endpoints.apiKeyParam + "&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob" + "&response_type=code" + "&scope=public"
case .getAccessToken:
return Endpoints.base + "/oauth/token" + Endpoints.apiKeyParam + "&\(Endpoints.apiSecretParam)" + "&redirect_uri=fancyimage:authenticate" + "&code=\(Auth.authorizationCode)" + "grant_type=Value authorization_code"
case .login:
return ""
}
}
var url: URL {
return URL(string: stringValue)!
}
}
class func getAccessToken(url: URL, completion: @escaping (Bool, Error?) -> Void ) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else {
DispatchQueue.main.async {
completion(false, error)
}
return
}
do {
let decoder = JSONDecoder()
let responseObject = try decoder.decode(TokenResponse.self, from: data)
Auth.accessToken = responseObject.accessToken
DispatchQueue.main.async {
completion(true, nil)
}
} catch {
DispatchQueue.main.async {
completion(false, error)
}
}
}
task.resume()
}
}
class LoginViewController: UIViewController {
@IBOutlet weak var loginViaWeb: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func loginViaWebsiteTapped(_ sender: Any) {
print(FancyClient.Endpoints.getAuthorizationCode.url)
UIApplication.shared.open(FancyClient.Endpoints.getAuthorizationCode.url, options: [:], completionHandler: nil)
// if let authorizationCode = responsePage.authorizationCode {
FancyClient.getAccessToken(url: FancyClient.Endpoints.getAccessToken.url) { (success, error) in
// continue...
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
