great place to work

Enabling CORS Authentication for Google Cloud Function with Python


Google Cloud Functions Python CORS

This article aims at throwing some light on the extensive research carried out by our product engineering team to fix the CORS (Cross-Origin Resource Sharing ) error while working on the Google Cloud Function in Python. It aims to help the developers to fix the issue effectively and in a reduced amount of time.

Google Cloud Functions with Python

The application overview is presented in a concrete and lucid manner. It is essentially a single page application SPA that had a front-end with Angular 9 and backend with Node.js application. With respect to the requirement we had for our application, we had to do run some experiments with a standard set of PDF data in order to retrieve it in a JSON format. While running the experiments, we realized that there were not any possible solutions when using the Node.js platform. Hence, we started exploring Python for a possible solution, as it has more number of existing modules that would suit our needs.

The Node.js application was deployed as a firebase application with the Firestore. It was like a connection between Angular and the Node.js. We had to fit in this Python code, somewhere in between, for the business logic to be implemented. All the business logic that had to be implemented was written in Python. It was successful and the result was as expected.  The next thing that we did was to deploy this Python code into the Google Cloud. We contemplated on how to deploy this particular Python code into the Google Cloud platform and zeroed down to two options. One was to go with the Firebase app and another one was to choose the Google Cloud function. Since we did not have any kind of backend required for this particular business logic to be implemented, we opted for the Google Cloud function.

Cross-Origin Resource Sharing (CORS)

At this point, there arose the Cross-origin resource sharing (CORS) issue when we try to integrate this with Angular front-end application invoking a Node.js app, which in turn invokes this particular Google Cloud function. Hence, we had to fix this issue with the use of the preflight headers as described in the Google Cloud documentation.

 What we did was to provide a preflight request with the method as OPTIONS and provided the access-control-allow-origin set to *. This cause issue was fixed and we were able to retrieve all the data perfectly.

Now, comes the authorization part. Since we did not have any kind of backend support for this particular business logic implemented, we had to depend on the Firebase application’s database URL that is the Firestore.

Why Google Cloud Functions for Python?

Cloud Functions are small pieces of code that execute in an event-driven manner. They do one small thing very efficiently in reaction to a trigger – usually an HTTP request. Since our application does not require any kind of backend for this particular business logic to be implemented, we moved to work on Google Cloud functions.

What is CORS?

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. CORS is a specification that enables truly open access across domain boundaries.


CORS in Google Cloud Functions

Most cloud functions, that are built on GCP, are been invoked from various other applications that reside in several other platforms. Hence, it is expected that most use cases will have to face the CORS issue.

To delve deeper into Python Google Cloud Function and know more about enabling CORS authentication, please follow the link: Python Google Cloud Function with CORS

Roopa Budda Thiagarajan,
Senior Software Engineer, RapidValue

Please Share Your Thoughts & Comments Below.

How can we help you?