权限不足错误

时间:2015-02-11 20:37:32

标签: c# google-prediction

我正在尝试编写一个控制台应用来使用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); 
        }
    }

1 个答案:

答案 0 :(得分: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); 
            }
        }