Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud Functions CORS error when called by AngularJS app.

Hello all,

I have no issue calling my function through Postman and I am able to see the 'Access-Control-Allow-Origin': '*' but whenever I am calling the function through my AngularJS app I receive a CORS error with the missing header Access-Control-Allow-Origin: '*'

This is the simple cloud function I am calling with bearer authentication below.

 

def hello(request):
    # Set CORS headers for the main request
    headers = {
            'Access-Control-Allow-Origin''*',
            'Access-Control-Allow-Methods''POST,GET',
            'Access-Control-Allow-Headers''Content-Type',
            'Access-Control-Max-Age''3600'
        }

    return ('Hello CORS'200, headers)
 
I am calling my function with an
$http({
          method: method,
          url: url,
          headers: headers
 }).then(successcallback,errorcallback)
 
Could it be that AngularJS is causing the issue? I would appreciate anyone's input on this and probably I just need someone else's perspective.
 
Thanks for your time!
 
 
0 3 2,512
3 REPLIES 3

Hello,

 

I believe the issue is that you are trying to call a private function. Does it work when you make it public? 

 

If that’s the case, then the reason is that are able to call the function with curl or similar tools (Postman) as it uses the bearer token, but in order to invoke a private function from a local environment, you need to obtain authentication credentials. If you try to use this code and you have the GOOGLE_APPLICATION_CREDENTIALS set:

“export GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/client_secret.json'”

 

You should be able to invoke your Cloud Function properly.

I thought I replied to this sorry. Thank you for responding to my question! Yes I am trying to call a private function. 
Before the app calls my private cloud function the UI first logs into google using the sign in page and the identity token is then passed through the Authorization: Bearer ID_TOKEN. 
I am not sure if this is related but the app is served in localhost using Grunt and I have added the middleware code in the Grunt.js file just to cover all my bases. 

Thanks again!

 this issue resolved, if so what is the workaround for this?