0
votes

This code work. It create a document kind entry. I only change content of variable:body(term='http://schemas.google.com/docs/2007#document' to term='http://schemas.google.com/docs/2007#file'). After sending request, server return response<400 Bad Request>. It's very strange. I traced Google Data API's source code, and I found the kind(http://schemas.google.com/docs/2007#file). How do I modify code to create file kind entry. Thanks.

HttpWebRequest call = null;
HttpWebResponse echo = null;

StreamWriter send = null;
StreamReader read = null;

string data = string.Empty;
string body = string.Empty;

body = "<?xml version='1.0' encoding='UTF-8'?>
    <entry xmlns='http://www.w3.org/2005/Atom'
        xmlns:docs='http://schemas.google.com/docs/2007'>
        <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/docs/2007#document'/>
        <title>test.txt</title>
    </entry>";

call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full") as HttpWebRequest;

call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");

send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();

echo = call.GetResponse() as HttpWebResponse;
if (echo.StatusCode == HttpStatusCode.Created)
{
    read = new StreamReader(echo.GetResponseStream());
    data = read.ReadToEnd();
    MessageBox.Show("entry.xml:" + Enviroment.NewLine + data);
    read.Close();
    read.Dispose();
}
else
{
    MessageBox.Show("entry not created. " + echo.StatusCode.ToString() + "(" + echo.StatusDescription + ")");
}
echo.Close();

Then, I wanna create a file kind entry. I modified code.

body = "<?xml version='1.0' encoding='UTF-8'?>
    <entry xmlns='http://www.w3.org/2005/Atom'
        xmlns:docs='http://schemas.google.com/docs/2007'>
        <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/docs/2007#file'/>
        <title>test.exe</title>
    </entry>";

call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full?convert=false") as HttpWebRequest;

call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");

send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();

echo = call.GetResponse() as HttpWebResponse;

return 400 Bad Request

body = "<?xml version='1.0' encoding='UTF-8'?>
    <entry xmlns='http://www.w3.org/2005/Atom'
        xmlns:docs='http://schemas.google.com/docs/2007'>
        <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/docs/2007#document'/>
        <title>test.exe</title>
    </entry>";

call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full?convert=false") as HttpWebRequest;

call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");

send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();

echo = call.GetResponse() as HttpWebResponse;

return 403 Forbidden

1

1 Answers

0
votes