Ii’m trying to share a Google Spreadsheet using C#. Because the Google Data 3.0 API hasn’t been ported to C# yet, I’ve been trying to interact with their RESTful web service. I haven’t worked with REST before.
Google asks for the following:
POST /feeds/default/private/full/<resource_id>/acl HTTP/1.1
Host: docs.google.com
GData-Version: 3.0
Authorization: <your authorization header here>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gAcl='http://schemas.google.com/acl/2007'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/acl/2007#accessRule'/>
<gAcl:role value='writer'/>
<gAcl:scope type='user' value='new_writer@example.com'/>
</entry>
I’m using the code below to try to access the web service, but it’s not working for me. What about this code is incorrect, given the requirements above?
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
entry = bunchaxml;
request.Method = "POST";
request.ContentType = "text/xml";
request.Headers["GData-Version"] = "3.0";
request.Credentials = new NetworkCredential(username, password);
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.WriteLine(entry.ToString());
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string sc = response.StatusCode.ToString();
string scd = response.StatusDescription.ToString();
}
catch (Exception e)
{
//LogError
}
The credentials you are using are not appropriate for the Google APIs. You can see how it is done here http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
Basically, you POST to a login endpoint with a your username and password and they return you a auth token that you can then use in future requests.