0

我正在尝试编写一个控制台应用程序来使用 Google 预测。我无法弄清楚我做错了什么。我不断收到“权限不足”。我怎样才能解决这个问题。如何查看实际生成的请求是什么?

    //Desired Request:  GET https://www.googleapis.com/prediction/v1.6/projects/1043149216958/trainedmodels/list?               
    //                      pageToken=%22%22&maxResults=5&key={YOUR_API_KEY}

    public async Task Run() 
    {
        UserCredential credential;
        using (var stream = new FileStream("Aggreate Volume 1 Client Secret.json", FileMode.Open, FileAccess.Read)) 
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] {PredictionService.Scope.DevstorageFullControl}, 
                "user", CancellationToken.None );
        }

        var service =
            new PredictionService(
                new BaseClientService.Initializer() {
                    HttpClientInitializer = credential,
                    ApplicationName = "Aggregate Volume 2"
                    }
                );

        try 
        {
            var response = service.Trainedmodels.List().Execute();
        }
        catch (Exception e) 
        { 
            Console.WriteLine("An error occurred: " + e.Message); 
        }
    }
4

1 回答 1

0

我通过进行一些更改来实现这一点。从 Google Developers Console 我得到以下信息(我选择使项目名称和产品名称相同。它们可以不同。):

    API:                Prediction API v1.6
    Project Name:       Aggregate Volumes MTFE
    Project ID:         golden-sentry-848
    Project No:         1043149216958
    Product Name:       Aggregate Volumes MTFE
    Predictive Model:   mtfe

API 版本非常重要。安装的包和使用语句都必须一致。包下载是

Install-Package Google.Apis.Prediction.v1_6

并且所需的 using 语句是:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Prediction.v1_6;
using Google.Apis.Prediction.v1_6.Data;
using Google.Apis.Services;

有一些命名问题让我有些困惑。当您初始化预测服务时,ApplicationName 是您在 Google Developers Console 中指定的产品名称。service.Trainedmodels.List 中需要的参数是您在 Google Developers Console 中创建项目时分配的项目编号。这是代码:

    public async Task 
    Run() {
        UserCredential credential;
        using (var stream = new FileStream("Aggregate Volumes MTFE Client Secret.json", FileMode.Open, FileAccess.Read)) {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] {PredictionService.Scope.Prediction},
                "user", CancellationToken.None );
            }

        var service =
            new PredictionService(
                new BaseClientService.Initializer() {
                    HttpClientInitializer = credential,
                    ApplicationName = "Aggregate Volumes MTFE"
                    }
                );

        try {
            var pre = service.Trainedmodels.List("1043149216958");
            var response = pre.Execute();
            }
        catch (Exception e) { 
            Console.WriteLine("An error occurred: " + e.Message); 
            }
        }
于 2015-02-13T19:31:57.697 回答