How to use google cloud rest apis
I recently encountered the scenario in my project to create a cloud run service and cloud composer environment using python. Unfortunately, GCP as of today is not providing the client libraries for some services unlike big query or cloud storage having the libraries which makes it easy for creating resources in google cloud. Fortunately, GCP does provide the REST API for almost all their services but the documentation is not quite good to understand the REST APIs.
In this blog, I will try to share my learnings on how to use the GCP APIs. It is a 2 step process first Authentication and second one is constructing the request payload and In this blog, I will show how to create a cloud run service but the process is the same for all other services also.
Authentication is a mandatory step for accessing the Google cloud APIs.
- For this, one service account with necessary IAM permissions is required
- Cloud run API should be enabled.
Generate the bearer token like shown in below snippet and use this token in request headers.
At first, I found it difficult to create the payload for creating a cloud run service. But, the documentation clearly arranges the payload structure with objects. Use this to create the request body as shown below.
Using this token and request body, cloud run service can be created via rest api.
For a complete example checkout the below github page.