# User Access Token

## User Access Token

The Whisk Graph API uses OAuth 2.0 to allow developers to get a user access token to access a single user’s data or do actions on their behalf. OAuth 2.0 is a specification outlined in [RFC 6749](https://tools.ietf.org/html/rfc6749) that allows third-party services to make requests on behalf of a user without accessing passwords and other sensitive information. If you are unfamiliar with OAuth 2.0, check out [Aaron Parecki’s "OAuth 2 Simplified" guide](https://aaronparecki.com/2012/07/29/2/oauth2-simplified).

**OAuth 2.0 Endpoints**

| Authorization Host      | <https://login.whisk.com>                    |
| ----------------------- | -------------------------------------------- |
| Authorization Endpoint  | <https://login.whisk.com/oauth/v2/authorize> |
| Token Exchange Endpoint | <https://login.whisk.com/oauth/v2/token>     |

## Authorization Code Grant

### Step 1: User Authorization

First, the user has to grant your app permission to access their data or do actions on their behalf. Whisk provides an authorization page where users can securely sign in with their Whisk username and password or accounts from other providers (Google, Facebook, etc.) to grant permissions. This authorization page is accessed through the authorization URL. To ensure that the driver grants permissions to your app properly, supply query parameters to that URL as described below.

```http
https://login.whisk.com/oauth/v2/authorize?scope=<REQUESTED_SCOPES>&client_id=<CLIENT_ID>&response_type=code&redirect_uri=<REDIRECT_URI>
```

| PARAMETER                     | DESCRIPTION                                                                                                                                              |
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| response\_type                | OAuth 2.0 response type. code                                                                                                                            |
| client\_id                    | The client ID of your application.                                                                                                                       |
| scope                         | The scope defines which part of user data will be accessed by your app. Your application can request multiple scopes as a space or comma-separated list. |
| redirect\_uri                 | The URI we will redirect back to after authorization by the resource owner.                                                                              |
| state (optional)              | State which will be passed back to you to prevent tampering.                                                                                             |
| identity\_provider (optional) | If provided, the authorization process is immediately forwarded to the specified identity provider (e.g. Google, Facebook)                               |

After you’ve supplied the needed parameters, present this authorization URL as a link for the user to visit.

### Step 2: Receive redirect

Once the Whisk user authenticates and authorizes your app, Whisk will issue an HTTP 302 redirect to the redirect\_uri passed in Step 1. On that redirect, you will receive a single-use authorization code that expires in 1 day.

```bash
GET https://your-redirect-uri/?code=<AUTHORIZATION_CODE>
```

This is the authorization code you need for the next step.

{% hint style="info" %}
In order for the redirect to work correctly, Whisk will need to approve the url. You will need to contact support in the dashboard.&#x20;
{% endhint %}

### Step 3: Get an access token

Use the **Token Exchange endpoint** to exchange the authorization code for an access\_token which will allow you to make requests on behalf of the user.

Example Request:

```bash
curl "https://login.whisk.com/oauth/v2/token" \
  -F "client_id=<CLIENT_ID>" \
  -F "client_secret=<CLIENT_SECRET>" \
  -F "grant_type=authorization_code" \
  -F "redirect_uri=<REDIRECT_URI>" \
  -F "code=<AUTHORIZATION_CODE_FROM_STEP_2>"
```

| PARAMETER      | DESCRIPTION                                                                |
| -------------- | -------------------------------------------------------------------------- |
| client\_id     | The Client ID of your application.                                         |
| client\_secret | The Client Secret of your application.                                     |
| grant\_type    | In this step, this should be set to authorization\_code                    |
| redirect\_uri  | The base of the URI must match the redirect\_uri used during Step 1 above. |
| code           | The authorization code from Step 2: Receive Redirect.                      |

**Example Response:**

```javascript
{
  "access_token": "Xb609ErrcoRNwll9wwxbk70OxFrbxEOu8Ui8ZzV4yJDA9RvLRGFhlMAAfkP2OmSS",
  "token_type": "Bearer",
  "expires_in": 2592000,
  "scope": [
    "cookbook:write"
  ],
  "refresh_token": "oS1437tty6buHTef0VGkXgcR7PvOt2DDUntSjWaCtDqJX8osK7d3Mip38NjpJdTH"
}
```

The access\_token is valid for the time described by expires\_in (in seconds). The refresh\_token expires after one year and can be used to obtain a new access\_token at any time given that your application is still authorized to access the API on behalf of this user.

The scope in the token response indicates what access was actually granted by user.

The access\_token retrieved from authenticating a user account will stay active even when a user changes the account password or other account details.

### Refreshing Tokens

If you requested the access token with the `offline_access`scope the response will include a `refresh_token`which can be used to refresh the access token. When the user’s `access_token`has expired, you can obtain a new `access_token`by exchanging the `refresh_token`associated with the `access_token`using the [Token Exchange endpoint](https://developers.whisk.com/docs/token-post).

Refreshing the user access token means that you don’t need to ask the user to authorize your app for the same permissions again (`offline_access`).

```bash
curl "https://login.whisk.com/oauth/v2/token" \
  -d "client_id=<CLIENT_ID>" \
  -d "client_secret=<CLIENT_SECRET>" \
  -d "grant_type=refresh_token" \
  -d "refresh_token=<REFRESH_TOKEN>"
```

A `refresh_token`is valid for one year and tokens that have been inactive for more than one year will be invalidated.

## Implicit Grant

Implicit flow is the less secure approach. Please consider Code Grant flow instead.

### Step 1: User Authorization

First, the user has to grant your app permission to access their data or do actions on their behalf. Whisk provides an authorization page where users can securely sign in with their Whisk username and password or accounts from other providers (Google, Facebook, etc.) to grant permissions. This authorization page is accessed through the authorization URL. To ensure that the driver grants permissions to your app properly, supply query parameters to that URL as described below.

```http
https://login.whisk.com/oauth/v2/authorize?client_id=<CLIENT_ID>&response_type=token&redirect_uri=<REDIRECT_URI>
```

| PARAMETER                     | DESCRIPTION                                                                                                                |
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| response\_type                | OAuth 2.0 response type. token                                                                                             |
| client\_id                    | The client ID of your application.                                                                                         |
| redirect\_uri                 | The URI we will redirect back to after authorization by the resource owner.                                                |
| state (optional)              | State which will be passed back to you to prevent tampering.                                                               |
| identity\_provider (optional) | If provided, the authorization process is immediately forwarded to the specified identity provider (e.g. Google, Facebook) |

After you’ve supplied the needed parameters, present this authorization URL as a link for the user to visit.

Unlike the authorization code grant type, in which the client makes separate requests for authorization and for an access token, the client receives the access token as the result of the authorization request.

Once the Whisk user authenticates and authorizes your app, Whisk will issues an access token and delivers it by adding the following parameters to the fragment component of the redirection URI using the `application/x-www-form-urlencoded` format

Example Response:

```http
http://example.com/#access_token=Xb609ErrcoRNwll9wwxbk70OxFrbxEOu8Ui8ZzV4yJDA9RvLRGFhlMAAfkP2OmSS&state=xyz&token_type=Bearer&expires_in=86400
```

The `access_token`is valid for the time described by `expires_in`(in seconds). The `refresh_token`does not return.

The `access_token`retrieved from authenticating a user account will stay active even when a user changes the account password or other account details.

## Use bearer token

You can pass the access\_token returned in the previous steps as a bearer token in the Authorization header, or pass it in as a query parameter in the URL.

Example: OAuth token sent in a header

```bash
curl "https://graph.whisk.com/v1/me" \
  -H "Authorization: Bearer <Access-Token>"
```
