Search code examples
google-apis-explorer

How to use API Explorer as a service account?


Here is the Google API Explorer for Google Drive / Files / List

https://developers.google.com/drive/api/v3/reference/files/list

I can access the account with a user account by logging in.

But I would like to access it as a service account to debug an issue with an API call there.

How will I access the API Explorer using a service account?

A service account is identified by an email address something like [email protected].


Solution

    • You want to use "Try this API" with the service account.

    I believe you want to achieve above. For this, how about the following answer?

    Issue and workaround:

    Unfortunately, "Try this API" can be used for the login account. So in this case, the service account cannot be used for "Try this API".

    So as a workaround, how about using the curl sample created by "Try this API"? The access token can be retrieved using the service account. When this access token is used for the curl sample created by "Try this API", you can test the API using the service account.

    Usage:

    1. Create curl sample.

    Please access to https://developers.google.com/drive/api/v3/reference/files/list. And please click a square button. You can see it at the following image.

    enter image description here

    By this, you can see the opened window as shown in the following image.

    enter image description here

    When you change the parameters for the API, the curl sample is also changed. Here, please copy the curl sample like below.

    curl \
      'https://www.googleapis.com/drive/v3/files?key=[YOUR_API_KEY]' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    2. Retrieve access token from service account.

    The sample scripts for retrieving the access token from the service account are as follows.

    The expiration time of access token is 1 hour. Please be careful this.

    3. Run the curl sample.

    Using the retrieved access token, you can test "Try this API" with the curl sample as follows.

    curl \
      'https://www.googleapis.com/drive/v3/files' \
      --header 'Authorization: Bearer ###' \
      --header 'Accept: application/json' \
      --compressed
    
    • In this case, please remove ?key=[YOUR_API_KEY]. Because the API key is not used for this.