我已使用Google Apps脚本API成功连接到我的Google表格和我的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);
}
}
}
}
Google Apps脚本:
function updateCells(projectName, projectManager)
{
Logger.log(projectName + ', ' + projectManager);
PropertiesService.getScriptProperties().setProperty('projectName', pName);
PropertiesService.getScriptProperties().setProperty('projectManager', pManager);
}
答案 0 :(得分: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'
});
此相关主题也可能有所帮助:
答案 1 :(得分:0)
我自己使用PropertiesService
时出现问题,通过添加未记录的(!!!)范围解决了问题:"https://www.googleapis.com/auth/script.storage"
。我希望他们有所有这些的文档(我已经看过this list,但它远未完成,并且通常缺少我实际需要的范围。)