【问题标题】:Authentication Failure when setting properties through Google Apps Script API (C#)通过 Google Apps Script API (C#) 设置属性时身份验证失败
【发布时间】:2017-07-24 21:00:46
【问题描述】:

我已使用 Google Apps Script API 通过我的 C# 应用程序成功连接到我的 Google Sheet。连接有效,但如果我添加以下几行:

PropertiesService.getScriptProperties().setProperty('projectName', pName);
PropertiesService.getScriptProperties().setProperty('projectManager', pManager);

对于我的 Google 脚本,我收到 401 未经授权的错误。为了在PropertiesService 中设置属性,我需要添加什么到我的范围?

我目前的范围是:

static string[] Scopes = { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets" };

C#代码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Script.v1;
using Google.Apis.Script.v1.Data;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PPR
{
    class googleAPI
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/script-dotnet-quickstart.json
        static string[] Scopes = { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets" };
        static string ApplicationName = "Google Apps Script Execution API .NET Quickstart";

        public googleAPI()
        {

    UserCredential credential;
                using (var stream =
                    new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
                {
                    string credPath = System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.Personal);
                    credPath = Path.Combine(credPath, ".credentials/script-dotnet-quickstart.json");

                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                    Console.WriteLine("Credential file saved to: " + credPath);
                }

                // Create Google Apps Script Execution API service.
                string scriptId = "MPD2B-0a8Q2KsDHoHVPh1HVhXBvIk9FTo";
                var service = new ScriptService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });
     ExecutionRequest request = new ExecutionRequest();
                //request.Function = "updateGlobals";
                request.Function = "updateCells";
                //request.Function = "callWeeklyHours";
                request.DevMode = true;

                IList<object> values = new List<object>();
                values.Add("tempProj");
                values.Add("Brett");
                request.Parameters = values;

                ScriptsResource.RunRequest runReq =
                        service.Scripts.Run(request, scriptId);

                try
                {
                    // Make the API request.
                    runReq.Execute();
                }
                catch (Google.GoogleApiException e)
                {
                    // The API encountered a problem before the script
                    // started executing.
                    Console.WriteLine("Error calling API:\n{0}", e);
                }
            }
        }
    }

Google Apps 脚本:

    function updateCells(projectName, projectManager)
    {    
      Logger.log(projectName + ', ' + projectManager);
      PropertiesService.getScriptProperties().setProperty('projectName', pName);
      PropertiesService.getScriptProperties().setProperty('projectManager', pManager);
}

【问题讨论】:

    标签: c# .net api google-apps-script google-sheets-api


    【解决方案1】:

    我自己在使用 PropertiesService 时遇到了问题,通过添加未记录的 (!!!) 范围解决了这个问题:"https://www.googleapis.com/auth/script.storage"。我希望他们有所有这些的文档(我见过 this list,但它远未完成,而且通常缺少我实际需要的范围)。

    【讨论】:

      【解决方案2】:

      您收到此错误的一个可能原因是您使用的是deprecated authentication method for Google APIs

      这是一个关于如何save the data的示例。

      // Set multiple script properties in one call.
      var scriptProperties = PropertiesService.getScriptProperties();
      scriptProperties.setProperties({
        'cow': 'moo',
        'sheep': 'baa',
        'chicken': 'cluck'
      });
      

      这个相关的帖子也可能有帮助:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        相关资源
        最近更新 更多