1

I am really confused. I am testing google task api, I can access my tasks but when I am tiring to insert a task it gives an error. in documentation this code is available, that dose not work because there is no Fetch function available.

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

I modified my code to,

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

But I am facing an error and Execute line:

An unhandled exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll

Additional information: Google.Apis.Requests.RequestError

Insufficient Permission [403]

ahmadabdullah247
  • 341
  • 1
  • 4
  • 16
  • 1
    Are you using the correct scope? https://www.googleapis.com/auth/tasks If you are using the scope: https://www.googleapis.com/auth/tasks.readonly you will be able to read but not to write new tasks. – Gerardo Dec 04 '15 at 22:29
  • @gerardo I changed the scope to TASK. Still not working – ahmadabdullah247 Dec 05 '15 at 02:28
  • the error specificly comes in: service.Tasks.Insert(task,taskList.Id.ToString()).Execute(); – ahmadabdullah247 Dec 05 '15 at 02:31
  • Does your code store the credentials after permissions are granted? If so, you need to delete those credentials and grant permissions again for the new scopes. It may be probable that you are using the previous tokens that have read only access even after changing tho the other scope. – Gerardo Dec 06 '15 at 06:33
  • @Gerardo how to check credentials ? – ahmadabdullah247 Dec 07 '15 at 11:20
  • use Google Accounts to revoke permission to your app, and then repeat your test. – pinoyyid Dec 08 '15 at 02:46
  • Have you got the solution for this? Facing same problem. @AhmadAbdullah – Sid Mar 21 '16 at 07:16

2 Answers2

1

You can copy following code lines from 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";

Please make sure that you use the same App name in your program and at the Google Console. And most important, as the code comment says, delete the .credentials directory.

The following Code works fine for me:

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

And you are right there is no method Fetch(), but as you can see I changed it to Execute() like you did and it works ;)

cranckstorm
  • 301
  • 1
  • 12
0

I had this problem too... I resolve this by getting de ID of the taskList first and passing into the insert method.

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

Works for me.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
Gevard
  • 1
  • 1