【问题标题】:Insufficient Permissions error权限不足错误
【发布时间】:2015-02-11 20:37:31
【问题描述】:

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

【问题讨论】:

    标签: c# google-prediction


    【解决方案1】:

    我通过进行一些更改来实现此功能。从 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); 
                }
            }
    

    【讨论】:

    • 您是否创建了一个新登录来回答您自己的问题?
    • 大卫,我不知道我是如何获得两次登录的。这当然不是我的本意。需要大量的实验和工作才能使代码正常运行。我的目的是给别人一个真正有效的起始位置。
    • 没关系。我重新格式化了你的问题,使它看起来更像普通的 C# 而不像 JavaScript,这可能会让一些读者感到困惑。
    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2018-08-24
    相关资源
    最近更新 更多