我真的很困惑。我正在测试google task api,我可以访问我的任务但是当我厌倦了插入任务时它会出错。在文档中,此代码可用,但由于没有可用的Fetch功能,因此无效。
process.env.UPDATED_DATE
我修改了代码,
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.GoogleApiException'未处理的例外情况发生在Google.Apis.dll
中其他信息:Google.Apis.Requests.RequestError
许可不足[403]
答案 0 :(得分:1)
您可以从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";
请确保您在程序中使用相同的应用程序名称 the Google Console。 最重要的是,正如代码注释所示,删除 .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()就像你做的那样,并且它有效;)< / p>
答案 1 :(得分:0)
我也有这个问题......
我通过首先获取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);
}
适合我。