このコードは機能します。ドキュメントの種類のエントリを作成します。variable:body(term ='http://schemas.google.com/docs/2007#document'をterm ='http://schemas.google.com/docs/2007#file')のコンテンツのみを変更します。リクエストを送信した後、サーバーはレスポンス<400BadRequest>を返します。とても不思議です。Google Data APIのソースコードをトレースしたところ、その種類(http://schemas.google.com/docs/2007#file)が見つかりました。コードを変更してファイルの種類のエントリを作成するにはどうすればよいですか。ありがとう。
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();
次に、ファイルの種類のエントリを作成します。コードを変更しました。
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;
400の悪いリクエストを返す
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;
403Forbiddenを返す