在任务列表中插入任务google任务api
本文关键字:任务 google api 插入 列表 | 更新日期: 2023-09-27 18:25:23
我真的很困惑。我正在测试谷歌任务api,我可以访问我的任务,但当我很累插入任务时,它会出错。在文档中,这段代码是可用的,但由于没有可用的Fetch函数,所以不起作用。
Task task = new Task { Title = "New Task"};
task.Notes = "Please complete me";
task.Due = "2010-10-15T12:00:00.000Z";
Task result = service.Tasks.Insert(task, "@default").Fetch();
Console.WriteLine(result.Title);
我将代码修改为
Google.Apis.Tasks.v1.Data.Task task = new Google.Apis.Tasks.v1.Data.Task { Title = "five" };
task.Notes = "Please complete me";
task.Due = DateTime.Now;
Google.Apis.Tasks.v1.Data.Task result = service.Tasks.Insert(task,taskList.Id.ToString()).Execute();
Console.WriteLine(result.Title);
但我面临一个错误和执行行:
Google.Apis.dll 中发生类型为"Google.GoogleApiException"的未处理异常
附加信息:Google.Apis.Requests.RequestError
权限不足[403]
您可以从Google文档中复制以下代码行:
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/tasks-dotnet-quickstart.json
static string[] Scopes = { TasksService.Scope.Tasks };
static string ApplicationName = "YOUR APPLICATION NAME";
请确保您在程序中使用相同的应用程序名称,并且谷歌控制台。最重要的是,正如代码注释所说,删除.credentials目录。
以下代码对我来说很好:
static void Main(string[] args)
{
UserCredential credential;
// Copy & Paste from Google Docs
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/tasks-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 Tasks API service.
var service = new TasksService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
TasklistsResource.ListRequest listRequest = service.Tasklists.List();
// Fetch all task lists
IList<TaskList> taskList = listRequest.Execute().Items;
Task task = new Task { Title = "New Task" };
task.Notes = "Please complete me";
task.Due = DateTime.Parse("2010-10-15T12:00:00.000Z");
task.Title = "Test";
// careful no verification that taskList[0] exists
var response = service.Tasks.Insert(task, taskLists[0].Id).Execute();
Console.WriteLine(response.Title);
Console.ReadKey();
}
你是对的,没有Fetch()方法,但正如你所看到的,我像你一样把它改成了Execute(),它很有效;)
我也遇到了这个问题。。。我通过首先获取taskList
的de-ID并传递到insert方法来解决这个问题。
public void IncluirTicket(string messageShort, string observacao)
{
// Define parameters of request.
listRequest = service.Tasklists.List();
listRequest.MaxResults = 10000;
// List task lists.
IList<TaskList> taskList = listRequest.Execute().Items;
var listaPendencias = taskList.Where(x => x.Title == "pendencias").ToList();
var listaPendenciasID = listaPendencias[0].Id;
Google.Apis.Tasks.v1.Data.Task task = new Google.Apis.Tasks.v1.Data.Task { Title = messageShort };
task.Notes = observacao;
task.Due = DateTime.Now;
Google.Apis.Tasks.v1.Data.Task result = service.Tasks.Insert(task, listaPendenciasID).Execute();
Console.WriteLine(result.Title);
}
适用于我。