VIKASH SHUKLA

VIKASH SHUKLA

  • NA
  • 16
  • 10.2k

Wanted to push some tasks on Google tasks.

Nov 18 2013 5:43 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OAuth2;
using Limilabs.Client.Authentication.Google;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using System.Diagnostics;
using System.Net;
using System.Text;
using Google.Apis.Tasks.v1;
using Google.Apis.Authentication.OAuth2;





public partial class NewDefault : System.Web.UI.Page
{
    List<string> scope;

    const string clientID = "518958397819.apps.googleusercontent.com";
    const string clientSecret = "HyNMCbxvC5GkK-aIZIuDsq4X";

    public const string OutOfBandCallbackUrl = "http://localhost:1500/new_google_tasks/NewDefault.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        AuthorizationServerDescription server1 = new AuthorizationServerDescription
        {
            AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
            TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
            ProtocolVersion = ProtocolVersion.V20,
            //RedirectUri = "http://localhost:1500/new_google_tasks/NewDefault.aspx"
            //OutOfBandCallbackUrl = "";
            
        };
        List<string> scope = new List<string>
        {
            "https://www.googleapis.com/auth/tasks"
 
        };
        if (Request.QueryString.Count > 0)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                WebServerClient consumer = new WebServerClient(server1, clientID, clientSecret);
                consumer.ClientIdentifier = clientID;
                consumer.ClientSecret = clientSecret;
                //consumer.ClientCredentialApplicator =
                //    ClientCredentialApplicator.PostParameter(clientSecret);
                IAuthorizationState grantedAccess = consumer.ProcessUserAuthorization(null);

                string accessToken = grantedAccess.AccessToken;

                Session["AccessToken"] = accessToken;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + accessToken);
                ASCIIEncoding encoding = new ASCIIEncoding();

                request.Method = "Post";
                request.Headers.Add("Authorization", "Bearer " + grantedAccess.AccessToken);


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                GoogleApi api = new GoogleApi(accessToken);



            }

        }
        else
        {

            WebServerClient consumer = new WebServerClient(server1, clientID, clientSecret);

            consumer.RequestUserAuthorization(scope, "online", new Uri("http://localhost:1500/new_google_tasks/NewDefault.aspx"));


            WebServerClient consumer1 = new WebServerClient(server1, clientID, clientSecret);
        }
       
    }

  
}




Getting error on line  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

i.e The remote server returned an error: (411) Length Required.

I m working on google tasks. plz help me to get my mistake.