【问题标题】:How to create file kind entry on Google Doc by protocol?如何通过协议在 Google Doc 上创建文件类型条目?
【发布时间】:2012-06-19 01:40:43
【问题描述】:

此代码有效。它创建一个文档种类条目。 我只将变量的内容:body(term='http://schemas.google.com/docs/2007#document' 更改为 term='http://schemas.google.com/docs/2007#file')。发送请求后,服务器返回响应。这很奇怪。我跟踪了 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();

然后,我想创建一个 file kind 条目。我修改了代码。

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;

返回 403 禁止

【问题讨论】:

    标签: c# gdata google-docs-api


    【解决方案1】:

    查看https://developers.google.com/google-apps/documents-list/#creating_or_uploading_files 的文档,您只需将?convert=false 添加到上传网址。

    【讨论】:

    • 我为schemas.google.com/docs/2007#file 添加了'?convert=false'。它仍然返回 400 Bad Request。我为schemas.google.com/docs/2007#document 添加了'?convert=false'。它仍然返回 403 Forbidden。
    • 尝试捕获与文档中的请求一起发送的 HTTP 请求。另外,为什么不使用 .NET 客户端库?
    • 我问了一个问题,你回答了我。(stackoverflow.com/questions/10962335/…) 但是 slug 无法修改条目标题。所以,我自己用.net来实现gdoc协议。现在,我修复了 gdoc 协议的所有问题,并且 gdoc 协议参考有一些错误,例如“更新文档时 POST 命令需要更改为 PUT 命令”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2015-10-22
    相关资源
    最近更新 更多