# Authentication - OAuth2

## OAuth 2.0 Authentication

**Purpose:**

OAuth 2.0 is used to confirm the identity of the user making the request. This ensures that only authenticated users can access resources.

**How it Works:**

1. **Get your Oauth2 credentials:**
    
    1. see [my credentials section](/smartdocflow/default/retrieve-my-connection-credentials)
        
2. **Obtain an Access Token:**
    
    1. Your client application initiates the OAuth 2.0 flow (client credentials grant) with the authorization server. See below examples on how to generate a token using command line or Postman.
        
    2. Upon successful authentication, the authorization server issues an access token.
        
3. **Include the Token in the Authorization Header:**
    
    1. The client application includes the access token in the `Authorization` header of each API request, formatted as `Bearer` .

Example of request including the Auth token in the Authorization header:
```
curl --location 'https://api.staging.smartdocflow.com/v1/exchange/file/submission?account=testuser1&destinations=testuser2' \
--header 'Content-Type: application/json' \
--header 'Authorization: eyJraWQiOiJGNFkwQmZLZkhnSmtSMjBGck1Ia3RyNTlNVTJjTUx5RlhZZUZ6WVNESFkwPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiIyMWRqZHJnN2xldDdhMW91cWtja3VhODQxcCIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoic21hcnRkb2NmbG93XC9yZWFkIHNtYXJ0ZG9jZmxvd1wvd3JpdGUiLCJhdXRoX3RpbWUiOjE3NDQxNjQ2NTAsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5ldS1jZW50cmFsLTEuYW1hem9uYXdzLmNvbVwvZXUtY2VudHJhbC0xX2duR1RWbGNNcSIsImV4cCI6MTc0NDE2ODI1MCwiaWF0IjoxNzQ0MTY0NjUwLCJ2ZXJzaW9uIjoyLCJqdGkiOiI1NTJlMWZlOS1kNDRhLTRjZmEtYTk3YS1mYzE2NDk2NDM4ZGUiLCJjbGllbnRfaWQiOiIyMWRqZHJnN2xldDdhMW91cWtja3VhODQxcCJ9.JSddi1oh7h3ydp8XKm8XxGanKiQXHYbjrL1JZYKlhSGzKfKCSEA10Inuwhk3NzMT1we4pidH2ENJ1u8xk3ROey62HadeKifm8BV9OPZIh0gifLsRMLLsrQsKQXckh11TcrIwI5_cedZN_p31nF7wJ08W5ctezHHciHd6oOoC44lbPwIkbtWTX1ChOr5qaTVb0-zyuetjd7diu60o-0pDnabDcrYRDgcr0Esx12yv27r3CxowITAFVgVkXdRWkjNqScJIEvNcXRIrtdeQhL7YiQY60AiLTvzSXuqHsIsOExm11sO-uMipvx_HT53kY1-e-m3uNkT-yUqkFvHGH3OrbA' \
--data '{
  "filename": "Example.txt",
  "checksum": "3ff91d026cbb5bceafec30bf8b7c26ab",
  "content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQgZm9yIGRlbW8gcHVycG9zZSBvbmx5LiByZXBsYWNlIHdpdGggeW91ciByZWFsIGZpbGUgY29udGVudC4="
}'
```

 
## Examples of Token generation
### How to create token with CURL in command line
Example of generating a token for Staging:
```
curl --location --request POST "https://accounts.staging.smartdocflow.com/oauth2/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=12qbfa57skm7gcl6dct16d8jc7" \
--data-urlencode "client_secret=********" \
--data-urlencode "scope=smartdocflow/read smartdocflow/write"
```


### How to create token using Postman

1. **Configure OAuth 2.0:**
    
    - Go to the "Authorization" tab in Postman.
        
    - Select "OAuth 2.0" from the "Type" dropdown.
        
    - Configure the OAuth 2.0 settings according to your API's requirements:
![image.png](/services/api/attachments/849c6e35-e0e9-4026-b910-595cfdeb1223)
        
        - **Grant Type:** Select the appropriate grant type. choose Client Credentials.

            
        - **Access Token URL:** The URL to exchange the authorization code for an access token.
            
        - **Client ID:** Your client application's ID .
            
        - **Client Secret:** Your client application's secret.
            
        - **Scope:** add needed scopes. Example "smartdocflow/read smartdocflow/write".
         
    - Click "Get New Access Token" to obtain a valid access token.
![image.png](/services/api/attachments/5af03749-948c-434d-b54e-4047724008f3)

  - If the request is successful, a new token is created. click use token to start using it.
 ![image.png](/services/api/attachments/4ae97d12-bd62-4a9f-84b8-70cd4d5bb91f)       

    - Postman will then automatically add the `Authorization: Bearer` header to your requests (where Bearer is your token value).
 ![image.png](/services/api/attachments/e0e4d643-5837-4aa8-b408-1782a25c66e6)

![image.png](/services/api/attachments/fb192801-0c9b-4b87-8a38-65b7678610e9)
