Obtaining an OAuth Token
This snippet demonstrates how your apps can use simple username and password, also known as resource owner password credentials (ROPC) to obtain an OAuth token.
Prerequisites
- Set up Xcode with the IBM Verify framework.
Steps
- Add a new Swift file to your project, and copy the following code snippet:
let hostname = URL(string: "https://yourserver/mga/sps/oauth/oauth20/token")!
let clientId = "yourClientId"
let username = "yourUserName"
let password = "yourPassword"
OAuthContext.shared.authorize(hostname, clientId, username: username, password: password)
{
token, error in
guard let _ = error else
{
print("Error: \(error.debugDescription)")
return
}
if let token = token
{
// We got the token.
print("Token: \(token)")
}
}
Next Steps
You have now successfully used resource owner password credentials (ROPC) to obtain an OAuth token in your iOS application, by using the IBMVerifyKit
framework.
For a more in-depth sample on OAuth tokens, have a look at the OAuth token sample under the Samples section. Furthermore, play around with other samples which demonstrate further functionality of the framework in the Samples section, or have a look at more quick, easy-to-implement code snippets in the Snippets section.