

# Obtain a token for your application with the client credentials grant

**Note**  
This is prerelease documentation for a service in preview release. It is subject to change.

The client credentials grant lets your application or agent call an external service as itself. There is no end user and no consent. Use this flow when the work is not tied to a specific person, such as a background job or a shared integration. Credential Broker obtains the token, caches it, and refreshes it, so your application retrieves a token with a single call.

## Key characteristics
<a name="credential-broker-m2m-characteristics"></a>

The client credentials grant has the following characteristics:
+ No end user and no consent prompt
+ Your application authenticates as itself with its own credentials
+ Permissions are defined at the application level rather than per user
+ Credential Broker caches and refreshes the token for you

## Example scenario
<a name="credential-broker-m2m-scenario"></a>

A reporting job that runs on Lambda reads shared account data from the company's Salesforce organization on a schedule, with no person signed in. The data belongs to the organization, not to a specific user. The job uses the client credentials grant so that it authenticates as itself.

1. **Priya (identity admin)** creates a credential provider pool and a credential provider that allows the client credentials grant and that connects to the company's Salesforce app, and grants the job's role permission to call the broker.

1. **Diego (application developer)** calls the broker from the job with the provider ARN and the scopes that the job needs. He writes no token handling and never sees the provider's client secret. Credential Broker returns a token that the job uses to call Salesforce.

## How it works
<a name="credential-broker-m2m-how"></a>

At runtime, your application obtains a token in a single call.

**To obtain a token with the client credentials grant**

1. Call `GetOAuthToken` with the client credentials grant, the provider ARN, and the scopes that you need. The response returns the access token and its expiration time.

1. On later calls, Credential Broker returns the cached token until it expires, then obtains and caches a new one. Your application repeats the same call each time it needs a token.

## Set up
<a name="credential-broker-m2m-setup"></a>

Before you run this flow, configure the following:

1. Create a credential provider pool.

1. Create a credential provider that allows the client credentials grant and that is registered with your outbound identity provider. You reference this provider by its ARN, in the form `arn:aws:credentialbroker:<region>:<account>:credentialproviderpool/<pool-id>/credentialprovider/<provider-id>`.

1. Grant the caller's role the `credentialbroker:` permissions for this flow. For more information, see [Control access to AWS IAM Credential Broker](credential-broker-permissions.md).

For a worked example that configures a credential provider and retrieves a token, see [Getting started: Set up your first credential provider](credential-broker-setup.md).

## Related resources
<a name="credential-broker-m2m-related"></a>
+ [Credential Broker API Reference](https://docs.aws.amazon.com/credentialbroker/latest/APIReference/)
+ [AWS CLI commands for Credential Broker](https://docs.aws.amazon.com/cli/latest/reference/credentialbroker/)