# OAuth 2.0 Credential Support

This guide provides example implementations of OAuth 2.0 credentials to enable secure authentication in clients generated by TypeSpec. With this approach, you can define OAuth 2.0 authentication directly in your TypeSpec files and supply custom credential implementations for a smooth, end-to-end authentication flow.

For more details, refer to the TypeSpec documentation: [Oauth2Auth](https://typespec.io/docs/libraries/http/reference/js-api/interfaces/oauth2auth/)

# Getting Started

## Define OAuth 2.0 in TypeSpec

In the .tsp file, declare the use of OAuth 2.0:

```python
@useAuth(OAuth2Auth<[
    {
      type: OAuth2FlowType.clientCredentials,
      authorizationUrl: "https://auth.example.com/authorize",
      tokenUrl: "https://auth.example.com/token",
      scopes: ["read", "write"],
    }
  ]>
)
```

## Implement the OAuth2 Credential

Follow the sample implementation to create a credential class that includes the `get_token_info` method, which returns an `AccessTokenInfo` object. This follows the credential interface defined in the [corehttp](https://learn.microsoft.com/python/api/overview/azure/corehttp-readme?view=azure-python-preview&preserve-view=true) package.

## Use the Credential with the Generated Client

```py
credential = MyOAuth2Credential()
client = MyGeneratedClient(endpoint, credential)

result = client.my_operation()
print(result)
```
