Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Nov 19, 2018
1 parent d3c28c6 commit 393b626
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Binary file added Docs/oauthsetup.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 19 additions & 8 deletions README.md
Expand Up @@ -84,7 +84,15 @@ public async Task can_get_spotprice_of_ETHUSD()

### Authentication Details
##### OAuth Access and Refresh Tokens
This section only applies to developers using **OAuth** authentication, not **API key + Secret** authentication. Full documentation for Coinbase's OAuth token flow can be found [here](https://developers.coinbase.com/docs/wallet/coinbase-connect/integrating). To summarize, obtaining an `AccessToken` from Coinbase is as follows:
This section only applies to developers using **OAuth** authentication, not **API key + Secret** authentication. Full documentation for Coinbase's **OAuth** token flow can be found [here](https://developers.coinbase.com/docs/wallet/coinbase-connect/integrating).

Before you begin **OAuth** you'll need to register your **OAuth** application with Coinbase. Once you have an **OAuth** application registered, you should have something similar to the following screen:

![OAuth AppSetup](https://raw.githubusercontent.com/bchavez/Coinbase/master/Docs/oauthsetup.png)

Note the `Client Id` and `Client Secret` values.

The steps to obtain an `AccessToken` from Coinbase user with your app is as follows:
1. First, get authorization from the user by sending the user to a URL using:
```csharp
//Create the options and permission scopes you want your app to have access to
Expand All @@ -103,34 +111,37 @@ var authUrl = OAuthHelper.GetAuthorizeUrl(opts);
2. The user will be presented with a screen similar to:
![OAuth Screen](https://developers.coinbase.com/images/docs/oauth-pongbot.png)

If your app needs more permissions, [check here for details](https://developers.coinbase.com/docs/wallet/coinbase-connect/permissions) and [here for reference](https://developers.coinbase.com/docs/wallet/coinbase-connect/reference).
If your app needs more permissions, [check here for details](https://developers.coinbase.com/docs/wallet/coinbase-connect/permissions) and [here for reference](https://developers.coinbase.com/docs/wallet/coinbase-connect/reference).

3. Once your app has been given permission, Coinbase will send the user's browser back to `RedirectUri`. In the query string parameter, a `code` will be present. Extract this `code` value in your application and use it to obtain an `AccessToken` as shown below:

```csharp
```csharp
//http://myserver.com/callback?code=f284bdc3c1c9e24a494e285cb387c69510f28de51c15bb93179d9c7f28705398&state=random
var redirectUri = "http://myserver.com/callback";
var code = "f284bdc3c1c9e24a494e285cb387c69510f28de51c15bb93179d9c7f28705398";

// Convert an Authorization Code to an Access Token.
// The RedirectUri parameter is the same parameter used in Step 1's AuthorizeOptions object above.
var token = await OAuthHelper.GetAccessTokenAsync(code, OAuthClientAppId, OAuthClientSecret, RedirectUri);
var token = await OAuthHelper.GetAccessTokenAsync(code, ClientId, ClientSecret, redirectUri);

var refreshToken = token.RefreshToken; // Save for later
var client = new CoinbaseClient(new OAuthConfig{ AccessToken = token.AccessToken })
```
```

###### Explicit Token Expiration and Renewal
`AccessToken`s have a two hour life time. Any **OAuth API** requests after after two hours will be denied. However, you can use a **Refresh Token** to get a new **Access Token** (that will again later, expire after 2 hours). Initially, when a `code` is converted into an access token. You actually get two tokens, an `AccessToken` and a `RefreshToken`. In the previous code example, the variable `refreshToken` is used to obtain a new `AccessToken`.
`AccessToken`s have a two hour life time. Any **OAuth API** requests after after two hours will be denied. However, you can use a **Refresh Token** to get a new **Access Token** (that will again later, expire after 2 hours). **Refresh Token**s don't have a life time per se, but they can only be *used once* to renew an expired **Access Token**.

Initially, back in **Step 3**, when an authorization `code` is converted into an access token, you actually get two tokens, an `AccessToken` and a `RefreshToken`. In **Step 3**, the variable `refreshToken` (which was saved for later use) is used to obtain a new `AccessToken`.

```csharp
var newToken = await OAuthHelper.RefreshTokenAsync(refreshToken, OAuthClientAppId, OAuthClientSecret);
var newToken = await OAuthHelper.RefreshTokenAsync(refreshToken, ClientAppId, ClientSecret);
var newClient = new CoinbaseClient(new OAuthConfig{ AccessToken = tokenNew.AccessToken })

// Safe for later, again because refresh tokens can only be used once for renewal.
var newRefreshToken = newToken.RefreshToken;
```

###### Automatic Token Renewal
The `CoinbaseClient` supports automatic token renewal. If you want to avoid refreshing your every two hours you can use the following `.WithAutomaticOAuthTokenRefresh()` extension method to activate automatic token renewal. When creating the `CoinbaseClient` object in **Step 3** above do the following:

Expand Down

0 comments on commit 393b626

Please sign in to comment.