2017-07-24 65 views
2

我已經使用Google Apps腳本API與我的C#應用​​程序成功連接到了我的Google表單。連接的工作,但如果我添加以下行:通過Google Apps Script API(C#)設置屬性時身份驗證失敗

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

谷歌Apps腳本:

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

回答

0

一個可能的原因,爲什麼你得到這個錯誤,如果你正在使用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' 
}); 

此相關的主題也可能有幫助:

0

我不得不使用PropertiesService自己一個問題,這是解決通過添加無證(!!!)範圍: "https://www.googleapis.com/auth/script.storage"。我希望他們有所有這些文件(我看過this list,但它遠遠沒有完成,通常缺少我實際需要的範圍)。