C# 权限不足错误

C# 权限不足错误,c#,google-prediction,C#,Google Prediction,我正在尝试编写一个控制台应用程序来使用谷歌预测。我不知道我做错了什么。我一直得到“权限不足”。我怎样才能解决这个问题。人们如何看到实际生成的请求 //Desired Request: GET https://www.googleapis.com/prediction/v1.6/projects/1043149216958/trainedmodels/list? // pageToken=%22%22&

我正在尝试编写一个控制台应用程序来使用谷歌预测。我不知道我做错了什么。我一直得到“权限不足”。我怎样才能解决这个问题。人们如何看到实际生成的请求

    //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); 
        }
    }

我通过做一些改变来实现这一点。从Google开发者控制台我得到了以下信息(我选择使项目名称和产品名称相同。它们可以不同):

API版本非常重要。安装的软件包和使用声明必须一致。下载的软件包是

Install-Package Google.Apis.Prediction.v1_6
所需的使用声明包括:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Prediction.v1_6;
using Google.Apis.Prediction.v1_6.Data;
using Google.Apis.Services;
有一些命名问题引起了我的一些困惑。初始化预测服务时,ApplicationName是您在Google开发者控制台中指定的产品名称。service.Trainedmodels.List中需要的参数是在Google开发者控制台中创建项目时分配的项目编号。代码如下:

    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); 
            }
        }

你创建了一个新的登录名来回答你自己的问题吗?大卫,我不知道我是怎么得到两个登录名的。那当然不是我的意图。要使代码正常运行,需要进行大量的实验和工作。我的目的是给别人一个真正有效的起始位置。没关系。我重新格式化了你的问题,使它看起来更像普通的C#,而不像JavaScript,这可能会让一些读者感到困惑。
    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); 
            }
        }