Hello everyone,

I am having hard time with google api I am using .net.I want to Insert 
update delete shared contacts in my web application but I am getting error 
unauthorized however if i change url I am getting response.

 below is my code.

 HttpWebResponse response = null;
        string sessionUrl = 
"https://www.google.com/m8/feeds/contacts/default/full";;
        Uri uri = new Uri(sessionUrl);
        HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;  
     
       request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Java/1.5.0_06";
        request.Host = "www.google.com";
        request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; 
q=.2";
        request.KeepAlive = true;
        request.Method = "GET";
        string header = 
formAuthorizationHeader(Session["newtoken"].ToString(), null, uri, "GET");
        request.Headers.Add(header);    
       response = request.GetResponse() as HttpWebResponse;

above code is working fine but if i change the URL 
by https://www.google.com/m8/feeds/contacts/mydomain.com/full I am getting 
error.

Please help me I just want to insert,update,delete shared contacts not 
personal contact.
I am also attaching the code for retrieving contacts.

Thanks
Foram

-- 
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html
?using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Security.Cryptography;
using System.Globalization;
using System.Text;
using System.Collections.Generic;
using Google.GData.Client;
public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            String token = Request.QueryString["token"];
            Session["token"] = token;
            HttpWebResponse response = null;
            string authSubToken = "";
            string sessionUrl = getSessionTokenUrl("https", "www.google.com");
            Uri uri = new Uri(sessionUrl);
            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
             Session["header"] = formAuthorizationHeader(Session["token"].ToString(), null, uri, "GET");
             request.Headers.Add(Session["header"].ToString());
            response = request.GetResponse() as HttpWebResponse;

            if (response != null)
            {
                int code = (int)response.StatusCode;
                if (code != 200)
                {

                }

                // get the body and parse it
                authSubToken = Utilities.ParseValueFormStream(response.GetResponseStream(), GoogleAuthentication.AuthSubToken);
            }



            Session["newtoken"]= authSubToken;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpWebResponse response = null;
        string sessionUrl = "https://www.google.com/m8/feeds/contacts/default/full";;
        Uri uri = new Uri(sessionUrl);
        HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
       
       request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Java/1.5.0_06";
        request.Host = "www.google.com";
        request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
        request.KeepAlive = true;
        request.Method = "GET";
        string header = formAuthorizationHeader(Session["newtoken"].ToString(), null, uri, "GET");
        request.Headers.Add(header);
       
        //request.Headers.Add(Session["header"].ToString());        
       response = request.GetResponse() as HttpWebResponse;



            
    }
    public static string getSessionTokenUrl(string protocol, string domain)
    {
        return protocol + "://" + domain + "/accounts/AuthSubSessionToken";
    }

    public static string formAuthorizationHeader(string token, AsymmetricAlgorithm key, Uri requestUri, string requestMethod)
    {
        if (key == null)
        {
            return String.Format(CultureInfo.InvariantCulture, "Authorization: AuthSub token=\"{0}\"", token);
        }
        else
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException("requestUri");
            }

            // Form signature for secure mode
            TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            int timestamp = (int)t.TotalSeconds;

            string nounce = generateULONGRnd();

            string dataToSign = String.Format(CultureInfo.InvariantCulture,
                                                "{0} {1} {2} {3}",
                                              requestMethod,
                                              requestUri.AbsoluteUri,
                                              timestamp.ToString(CultureInfo.InvariantCulture),
                                              nounce);


            byte[] signature = sign(dataToSign, key);

            string encodedSignature = Convert.ToBase64String(signature);

            string algorithmName = key is DSACryptoServiceProvider ? "dsa-sha1" : "rsa-sha1";

            return String.Format(CultureInfo.InvariantCulture,
                                    "Authorization: AuthSub token=\"{0}\" data=\"{1}\" sig=\"{2}\" sigalg=\"{3}\"",
                                 token, dataToSign, encodedSignature, algorithmName);
        }

    }

    private static string generateULONGRnd()
    {
        byte[] randomNumber = new byte[20];

        // Create a new instance of the RNGCryptoServiceProvider. 
        RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();

        // Fill the array with a random value.
        Gen.GetBytes(randomNumber);

        StringBuilder x = new StringBuilder(20);
        for (int i = 0; i < 20; i++)
        {
            if (randomNumber[i] == 0 && x.Length == 0)
            {
                continue;
            }
            x.Append(Convert.ToInt16(randomNumber[i], CultureInfo.InvariantCulture).ToString()[0]);
        }
        return x.ToString();
    }

    //////////////////////////////////////////////////////////////////////
    private static byte[] sign(string dataToSign, AsymmetricAlgorithm key)
    {
        byte[] data = new ASCIIEncoding().GetBytes(dataToSign);

        try
        {
            RSACryptoServiceProvider providerRSA = key as RSACryptoServiceProvider;
            if (providerRSA != null)
            {
                return providerRSA.SignData(data, new SHA1CryptoServiceProvider());
            }
            DSACryptoServiceProvider providerDSA = key as DSACryptoServiceProvider;
            if (providerDSA != null)
            {
                return providerDSA.SignData(data);
            }
        }
        catch (CryptographicException e)
        {
            // Tracing.TraceMsg(e.Message);
        }
        return null;
    }

}

Reply via email to