您可以从 Google Docs 复制以下代码行:
// 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";
请确保您在程序和
Google 控制台中使用相同的应用程序名称。最重要的是,正如代码注释所说,删除.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()并且它有效;)