使用Google Docs API

时间:2011-10-13 09:28:51

标签: c# php google-docs-api

我想了解如何将远程文档上传到Google文档,以及如何使用Google文档API使用Google文档查看该文档。

当我上传文件时,是否回复了docid?因此,我可以在上传到我的帐户后使用它在线查看文档。

例如,我可以使用docid

打开我帐户中的一个文档

https://docs.google.com/Doc?docid=0AdmWTLTOoWVBZGd3ZDdtZmhfMGZ3czNrNmho

这可以在PHP中实现吗?我听说它不再支持了。我更喜欢PHP而不是C#,但如果不支持PHP,我可以使用C#。

1 个答案:

答案 0 :(得分:0)

您必须使用以下代码下载本地文件。

var request = new DocumentsRequest(settings);
request.BaseUri = "https://docs.google.com/feeds/default/private/full/folder" + folderResourceId + "/contents";
Feed<Document> feed = request.GetEverything();

foreach (Document entry in feed.Entries)
{
    if (entry.Title == fileName)
    {
        var fI = new FileInfo(uploadpath + entry.Title);
        Stream stream = request.Download(entry, "");
        arr = Read(stream);
        stream.Close();
        break;
     }
}

private Byte[] Read(Stream stream)
{
    //long originalPosition = stream.Position;
    //stream.Position = 0;

    try
    {
        Byte[] readBuffer = new Byte[4096];

        int totalBytesRead = 0;
        int bytesRead;

        while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
        {
            totalBytesRead += bytesRead;

            if (totalBytesRead == readBuffer.Length)
            {
                int nextByte = stream.ReadByte();
                if (nextByte != -1)
                {
                    Byte[] temp = new Byte[readBuffer.Length * 2];
                    Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                    Buffer.SetByte(temp, totalBytesRead, (Byte)nextByte);
                    readBuffer = temp;
                    totalBytesRead++;
                }
            }
        }

        Byte[] buffer = readBuffer;
        if (readBuffer.Length != totalBytesRead)
        {
            buffer = new Byte[totalBytesRead];
            Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
        }
        return buffer;
    }
    finally
    {
        //stream.Position = originalPosition;
    }
}

下载文件后,将其保存到本地系统并使用asp.net显示