Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Getting the Access Token

johnnycoinbase edited this page Jan 10, 2019 · 1 revision

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:

  1. In Xcode, click on your project in the Project Navigator
  2. Select your app's target
  3. Click Info
  4. Open URL Types
  5. Click "+" to create a new URL Type
  6. 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
    }

Note

If you want to know more on how to enable token auto-refresh for Coinbase, please read Token refreshing

Clone this wiki locally