invoking google cloud function from Colab, authentication issues

Viewed 31

I am invoking a google cloud function from Google Colab, it works very well when I use a service account and key json file like this

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] ='/xxxxxxx.json'

I want to share the colab notebook and prefer that user do their own authentication, I tried this code

from google.colab import auth
auth.authenticate_user()

when I run the function

import json
import pandas as pd
import requests
import google.auth.transport.requests
import google.oauth2.id_token
audience = 'https://xxxxxxxxxxxxxxxxx'
request = google.auth.transport.requests.Request()
TOKEN = google.oauth2.id_token.fetch_id_token(request, audience)
def Duckit(SQL):
    r = requests.post(
    audience, 
    headers={'Authorization': f"Bearer {TOKEN}", "Content-Type": "application/json"},
    data=json.dumps({"name": SQL})  
    )
    return pd.read_json(json.loads(r.content))

I get this error

TransportError: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=https%3A%2F%2Fxxxxxxa.run.app&format=full
 from the Google Compute Enginemetadata service. Status: 501 Response:\nb'unimplemented\\n'", <google.auth.transport.requests._Response object at 0x7f20e1576910>)
0 Answers
Related