Purry
Purry

Reputation: 241

Trouble with Google Application Credentials

Hi there first and foremost this is my first time using Googles services. I'm trying to develop an app with the Google AutoML Vision Api (Custom Model). I have already build a custom model and generated the API keys(I hope I did it correctly tho).

After many attempts of developing via Ionics & Android and failing to connect to the to the API.

I have now taken the prediction modelling given codes in Python (on Google Colab) and even with that I still get an error message saying that Could not automatically determine credentials. I'm not sure where I have gone wrong in this. Please help. Dying.

#installing & importing libraries 

!pip3 install google-cloud-automl

import sys  

from google.cloud import automl_v1beta1
from google.cloud.automl_v1beta1.proto import service_pb2


#import key.json file generated by GOOGLE_APPLICATION_CREDENTIALS
from google.colab import files
credentials = files.upload()


#explicit function given by Google accounts 

[https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-python][1]

def explicit():
from google.cloud import storage

# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json(credentials)

# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)




#import image for prediction
from google.colab import files
YOUR_LOCAL_IMAGE_FILE = files.upload()


#prediction code from modelling
def get_prediction(content, project_id, model_id):
prediction_client = automl_v1beta1.PredictionServiceClient()

name = 'projects/{}/locations/uscentral1/models/{}'.format(project_id, 
        model_id)
payload = {'image': {'image_bytes': content }}
params = {}
request = prediction_client.predict(name, payload, params)
return request  # waits till request is returned

#print function substitute with values 
 content = YOUR_LOCAL_IMAGE_FILE
 project_id = "REDACTED_PROJECT_ID"
 model_id = "REDACTED_MODEL_ID"

 print (get_prediction(content, project_id,  model_id))

Error Message when run the last line of code:

enter image description here

Upvotes: 0

Views: 1219

Answers (1)

Edo Akse
Edo Akse

Reputation: 4401

credentials = files.upload()
storage_client = storage.Client.from_service_account_json(credentials)

these two lines are the issue I think. The first one actually loads the contents of the file, but the second one expects a path to a file, instead of the contents.

Lets tackle the first line first: I see that just passing the credentials you get after calling credentials = files.upload() will not work as explained in the docs for it. Doing it like you're doing, the credentials don't actually contain the value of the file directly, but rather a dictionary for filenames & contents.

Assuming you're only uploading the 1 credentials file, you can get the contents of the file like this (stolen from this SO answer):

from google.colab import files
uploaded = files.upload()
credentials_as_string = uploaded[uploaded.keys()[0]]

So now we actually have the contents of the uploaded file as a string, next step is to create an actual credentials object out of it.

This answer on Github shows how to create a credentials object from a string converted to json.

import json
from google.oauth2 import service_account

credentials_as_dict = json.loads(credentials_as_string)
credentials = service_account.Credentials.from_service_account_info(credentials_as_dict)

Finally we can create the storage client object using this credentials object:

storage_client = storage.Client(credentials=credentials)

Please note I've not tested this though, so please give it a go and see if it actually works.

Upvotes: 1

Related Questions