Skip to content

Instantly share code, notes, and snippets.

@aalemi97
Last active October 14, 2020 10:09
Show Gist options
  • Save aalemi97/efed1d057de7220bb0acae4c524c2fb5 to your computer and use it in GitHub Desktop.
Save aalemi97/efed1d057de7220bb0acae4c524c2fb5 to your computer and use it in GitHub Desktop.
func refreshToken(completion: @escaping (_ isSuccess: Bool) -> Void) {
guard let apiKey = UserDefaultsManager.shared.getUserCredentials().apiKey,
let secretKey = UserDefaultsManager.shared.getUserCredentials().secretKey else { return }
let parameters = ["grant_type": "client_credentials", "client_id": apiKey, "client_secret": secretKey]
AF.request(authorize, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
if let data = response.data, let token = (try? JSONSerialization.jsonObject(with: data, options: [])
as? [String: Any])?["access_token"] as? String {
UserDefaultsManager.shared.setToken(token: token)
print("\nRefresh token completed successfully. New token is: \(token)\n")
completion(true)
} else {
completion(false)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment