Setting up authentication

V2 of the API relies on a Google Cloud Platform Service Account for authentication, instead of the previously used client and developer access tokens. A Service Account is automatically provisioned when you create an agent in Dialogflow and enable the V2 API.

Getting the Service Account key

In order to use the V2 API, you will need to create a new Service Account and obtain a private key associated with the Service Account. The key is downloaded as a JSON file upon creation of the Service Account.

  1. Click on the gear icon, to the right of the agent name.

  2. Under the GOOGLE PROJECT section, click on the name of the Service Account.

    This will take you to the Google Cloud Platform Service Accounts page, but you first need to update the Service Account's role.

  3. Click on the menu button in the upper left hand corner and click on IAM & admin.

  4. Click on Service Accounts in the left hand menu.

  5. Click on the Create Service Account button at the top of the page.

  6. In the pop up, enter a name for the service account.

  7. Click on Role. Under the Dialogflow category, select the desired role.

    Role name Access description
    Dialogflow API Admin Full API access
    Dialogflow API Client Session level API access
    Dialogflow API Reader Read-only API access

  8. Check the Furnish a new private key option and make sure JSON is selected for Key type.

  9. Click the Create button.

  10. Download of the JSON file will start. Choose a location to save it and confirm.

  11. Once complete, you'll see a pop up with a confirmation message. Click Close.

Using the key

In order to call the API using the key you just generated, you'll need to provide the key using the Google Cloud SDK.

  1. If you haven't already, install and initialize the Cloud SDK.

  2. Activate the service account in your local environment:

        gcloud auth activate-service-account --key-file=<service-account-key-file.json>
    
  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. For use in future shell sessions, you should save this setting in an initialization file or system setting, such as in a .bashrc file.

Example request

HTTP

    POST https://dialogflow.googleapis.com/v2beta1/projects/project-name/agent/intents

    Headers:
    Authorization: Bearer $(gcloud auth print-access-token)
    Content-Type: application/json

    POST body:
    {
        'displayName': 'StartStopwatch',
        'priority': 500000,
        'mlEnabled': true,
        'trainingPhrases': [
            {
                'type': 'EXAMPLE',
                'parts': [
                    {
                        'text': 'start stopwatch'
                    }
                ]
            }
        ],
        'action': 'start',
        'messages': [
            {
                'text': {
                    'text': [
                        'Stopwatch started'
                    ]
                }
            }
        ],
    }

cURL

    curl -X POST -H "Authorization: Bearer "$(gcloud auth print-access-token) \
    -H "Content-Type: application/json; charset=utf-8" \
    "https://dialogflow.googleapis.com/v2beta1/projects/project-name/agent/intents" \
    --data "{
        'displayName': 'StartStopwatch',
        'priority': 500000,
        'mlEnabled': true,
        'trainingPhrases': [
            {
                'type': 'EXAMPLE',
                'parts': [
                    {
                        'text': 'start stopwatch'
                    }
                ]
            }
        ],
        'action': 'start',
        'messages': [
            {
                'text': {
                    'text': [
                        'Stopwatch started'
                    ]
                }
            }
        ],
    }"