首页 > 解决方案 > google.api_core.exceptions: 403 调用者没有权限

问题描述

我正在使用google cloud automl tables api,当我对该方法执行url请求时,它给了我这个错误:

google.api_core.exceptions.PermissionDenied: 403 调用者没有权限

参数是正确的,为什么它告诉我那个错误!

   @app.route('/model_train',methods=['GET','POST'])
def model_train():

    project_id = 'myproject'
    compute_region = 'us-central1'
    model_id = 'TBL7912987273010'
    file_path = 'E:/downloads/Dataset-entrainement-demain_ai_demonstrateur_attrition_Oct19_Copie.csv'
    score_threshold = '0.5'

    automl_client = automl.AutoMlClient()

    model_full_id = automl_client.model_path(
        project_id, compute_region, model_id
    )

    prediction_client = automl.PredictionServiceClient()

    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    with open(file_path, "rt") as csv_file:
        content = csv.reader(csv_file)
        for row in content:
            values = []
            for column in row:
                values.append({'number_value': float(column)})
            payload = {
                'row': {'values': values}
            }

            response = prediction_client.predict(model_full_id, payload)
            print("Prediction results:")
            for result in response.payload:
                print("Predicted class name: {}".format(result.display_name))
                print("Predicted class score: {}".format(result.classification.score))

谁能帮帮我!

标签: pythongoogle-cloud-automl

解决方案


添加一个正式的答案,基本上问题在于 GCP IAM 部分为用户帐户设置的权限。首先这里需要为项目启用automl API

然后,您需要生成凭据并将它们作为环境变量传递。您可以使用 os 库在文件顶部添加环境变量:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"

推荐阅读