VIKASH SHUKLA

VIKASH SHUKLA

  • NA
  • 16
  • 10.2k

To push some tasks to Google Tasks

Dec 2 2013 7:06 AM
I m working on Google Tasks after coding I find that its work on VS 2012 not on VS 2010.


I pasted my code below 


using System;
using System.Reflection;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Tasks.v1;
using Google.Apis.Tasks.v1.Data;
using Google.Apis.Util.Store;

public partial class Default2 : System.Web.UI.Page
{
    private TasksService service;

        // Application logic should manage users authentication. This sample works with only one user. You can change
        // it by retrieving data from the session.
        private const string UserId = "user-id";

        protected void Page_Load(object sender, EventArgs e)
        {
            GoogleAuthorizationCodeFlow flow;
            
            flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
           {
               DataStore = new SessionDataStore(Session),
               ClientSecrets = new ClientSecrets() { ClientId = "381102426727-ltgvsud3pkpdqet43hs59p6n1nseaioo.apps.googleusercontent.com", ClientSecret = "BhUVW4QG-ZMVIAelClKIg4dj" },
               Scopes = new[] { TasksService.Scope.TasksReadonly, TasksService.Scope.Tasks }
           });

            //var uri = Request.Url.ToString();
            var uri = "http://localhost:55149/GoogleTaskSample/Default2.aspx";
            var code = Request["code"];
            if (code != null)
            {
                var token = flow.ExchangeCodeForTokenAsync(UserId, code, uri, CancellationToken.None).Result;

                // Extract the right state.
                var oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Request["state"]).Result;
                Response.Redirect(oauthState);
            }
            else
            {
                var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result;
                if (result.RedirectUri != null)
                {
                    // Redirect the user to the authorization server.
                    Response.Redirect(result.RedirectUri);
                }
                else
                {
                    // The data store contains the user credential, so the user has been already authenticated.
                    service = new TasksService(new BaseClientService.Initializer
                    {
                        ApplicationName = "TempApp",
                        HttpClientInitializer = result.Credential
                    });
                    service.Tasks.Insert(new Task() { Title = "Vikas" }, "@default").Execute();
                }
            }
        }

        /// <summary>Gets the TasksLists of the user.</summary>
        public async System.Threading.Tasks.Task FetchTaskslists()
        {
            try
            {
                // Execute all TasksLists of the user asynchronously.
                TaskLists response = await service.Tasklists.List().ExecuteAsync();
                ShowTaskslists(response);
            }
            catch (Exception ex)
            {
                var str = ex.ToString();
                str = str.Replace(Environment.NewLine, Environment.NewLine + "<br/>");
                str = str.Replace("  ", " &nbsp;");
                output.Text = string.Format("<font color=\"red\">{0}</font>", str);
            }
        }

        private void ShowTaskslists(TaskLists response)
        {
            if (response.Items == null)
            {
                output.Text += "You have no task lists!<br/>";
                return;
            }

            output.Text += "Showing task lists...<br/>";
            foreach (TaskList list in response.Items)
            {
                Panel listPanel = new Panel() { BorderWidth = Unit.Pixel(1), BorderColor = Color.Black };
                listPanel.Controls.Add(new Label { Text = list.Title });
                listPanel.Controls.Add(new Label { Text = "<hr/>" });
                listPanel.Controls.Add(new Label { Text = GetTasks(list) });
                lists.Controls.Add(listPanel);
            }
        }

        private string GetTasks(TaskList taskList)
        {
            var tasks = service.Tasks.List(taskList.Id).Execute();
            if (tasks.Items == null)
            {
                return "<i>No items</i>";
            }

            var query = from t in tasks.Items select t.Title;
            return query.Select((str) => "&bull; " + str).Aggregate((a, b) => a + "<br/>" + b);
        }

        protected async void listButton_Click(object sender, EventArgs e)
        {
            await FetchTaskslists();
        }

        protected void ButtonClearDataStore_Click(object sender, EventArgs e)
        {
            var x = new SessionDataStore(Session);
            x.ClearAsync();
        }
}