2015-12-02 40 views
1

我真的很困惑。我正在測試谷歌任務api,我可以訪問我的任務,但是當我厭倦插入任務時,它會給出錯誤。在文檔中這個代碼是可用的,這是不行的,因爲沒有可用的提取功能。在任務列表中插入任務google-task-api

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.GoogleApiException' 發生在Google.Apis。 dll

附加信息:Google.Apis.Requests.RequestError

足夠的許可[403]

+1

你使用正確的範圍? https://www.googleapis.com/auth/tasks 如果您使用的範圍:https://www.googleapis.com/auth/tasks.readonly,您將能夠閱讀,但不能編寫新任務。 – Gerardo

+0

@gerardo我將範圍更改爲TASK。仍然不能正常工作 –

+0

錯誤具體出現在: service.Tasks.Insert(task,taskList.Id.ToString()).execute(); –

回答

1

您可以從以下文檔谷歌代碼行復制:

// 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使用相同的應用程序名稱。 最重要的是,如代碼註釋所述,刪除。憑證目錄。

下面的代碼工作正常,我:

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(); 
    } 

而且你是正確的,沒有方法取(),但你可以看到我把它改爲執行()像你這樣它的工作原理;)