【问题标题】:Trouble with Google Application CredentialsGoogle 应用程序凭据问题
【发布时间】:2019-04-16 07:38:37
【问题描述】:

您好,首先这是我第一次使用 Google 的服务。我正在尝试使用 Google AutoML Vision Api(自定义模型)开发应用程序。我已经构建了一个自定义模型并生成了 API 密钥(我希望我做得正确)。

经过多次尝试通过 Ionics 和 Android 进行开发,但未能连接到 API。

我现在已经在 Python(在 Google Colab 上)中使用了给定代码的预测建模,即使这样我仍然收到一条错误消息,指出无法自动确定凭据。我不确定我在哪里出错了。请帮忙。快死了。

#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))

运行最后一行代码时的错误信息:

【问题讨论】:

    标签: python api authentication google-vision


    【解决方案1】:
    credentials = files.upload()
    storage_client = storage.Client.from_service_account_json(credentials)
    

    这两行是我认为的问题。 第一个实际加载文件的内容,但第二个需要文件的路径,而不是内容。

    让我们先处理第一行: 我看到在调用credentials = files.upload() 后通过credentials 将无法正常工作,如the docs for it 中所述。像你一样做,credentials 实际上并不直接包含文件的值,而是文件名和内容的字典。

    假设你只上传了 1 个凭证文件,你可以像这样(stolen from this SO answer)

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

    所以现在我们实际上将上传文件的内容作为一个字符串,下一步是用它创建一个实际的凭据对象。

    This answer on Github 展示了如何从转换为 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)
    

    最后我们可以使用这个凭证对象创建存储客户端对象:

    storage_client = storage.Client(credentials=credentials)
    

    请注意,我还没有对此进行测试,所以请试一试,看看它是否真的有效。

    【讨论】:

    • 您好,谢谢您的回复。我放弃了尝试解决问题,因此我改用 Nanonets,它就像一个魅力,实际上更便宜。 nanonets.com
    猜你喜欢
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2017-10-12
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多