Ich habe mithilfe der Google Apps Script-API erfolgreich eine Verbindung zu meinem Google-Blatt mit meiner C# -Anwendung hergestellt. Die Verbindung funktioniert, aber wenn ich die folgenden Zeilen anschließe:Authentifizierung Fehler beim Festlegen von Eigenschaften über Google Apps Script-API (C#)
PropertiesService.getScriptProperties().setProperty('projectName', pName);
PropertiesService.getScriptProperties().setProperty('projectManager', pManager);
Zu meinem Google Script bekomme ich einen 401 nicht autorisierten Fehler. Was muss ich meinen Bereichen hinzufügen, um Eigenschaften in der PropertiesService
festzulegen?
Mein aktueller Anwendungsbereich ist:
static string[] Scopes = { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets" };
C# -Code:
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 Script:
function updateCells(projectName, projectManager)
{
Logger.log(projectName + ', ' + projectManager);
PropertiesService.getScriptProperties().setProperty('projectName', pName);
PropertiesService.getScriptProperties().setProperty('projectManager', pManager);
}