-
Notifications
You must be signed in to change notification settings - Fork 67
Getting the Access Token
To use OAuth2 you will need to add a custom URI scheme to your application. It is recommended that this URI scheme starts with your app's bundle identifier. To add a URI scheme:
- In Xcode, click on your project in the Project Navigator
- Select your app's target
- Click Info
- Open URL Types
- Click "+" to create a new URL Type
- Enter your new URL scheme in URL Schemes
You now need to create an OAuth2 application for your iOS application at https://www.coinbase.com/oauth/applications.
-
Click
+ New OAuth2 Application
and enter a name for your application. -
In
Permitted Redirect URIs
, you should enter your redirect URI which should follow next pattern:<your_scheme>://<some_name>
For example, if your custom URI scheme is
"com.example.app"
, then you can enter next redirect URI:com.example.app://callback
-
Fill in the rest of required fields.
-
Save the application and note the Client ID and Client Secret.
You can now integrate the OAuth2 sign in flow into your application.
First, you should call configure
method on oauth
property of Coinbase instance to set all required properties before calling any authorization method:
coinbase.oauth.configure(clientID: <client_id>,
clientSecret: <client_secret>,
redirectURI: <redirect_uri>)
Then you can initiate OAuth flow by calling beginAuthorization
method with required scopes and other parameters
try coinbase.oauth.beginAuthorization(scope: [Scope.Wallet.Accounts.read, ...])
It will redirect user into Safari browser to continue authentication.
In AppDelegate
you should handle redirection back by calling coinbase.oauth.completeAuthorization
method inside application(_:open:options:)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handleCoinbaseOAuth = coinbase.oauth.completeAuthorization(url) { result in
// setup coinbase, e.g. coinbase.accessToken = result.value?.accessToken
}
return handleCoinbaseOAuth
}
If you want to know more on how to enable token auto-refresh for Coinbase, please read Token refreshing