Google文档:无法使用C#中的管理访问/模拟(禁止403)导出/下载用户文档

时间:2012-06-09 00:38:01

标签: c# google-docs impersonation

我整天都在为此敲打脑袋,做了大量谷歌搜索,但无济于事。我正在编写一个应用程序,将所有用户的Google Docs文件下载到本地磁盘。我必须为几个用户执行此操作,并且仅提供管理员帐户(通过模拟使用)。该问题仅适用于模拟用户未编写/共享的文档。在所有情况下,获取标题信息都可以正常工作(无论作者和共享设置如何)。下载由模拟帐户创作的文件也可以正常工作。但是,下载其他用户创作的文件失败,HTTP 401或403(权限被拒绝)。有人可以告诉我我做错了什么吗?下面是代码的修剪版本。谢谢!

    using System.IO;
using Google.GData.Documents;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Documents;


void impersonateAndGetAllUsersDocs()
{
    string applicationName = "myapp";
    string username = "admin@domain.com";
    string password = "adminPassword";
    string accountToImpersonate = "someOtherUser@domain.com";

    DocRequest = new DocumentsRequest(new RequestSettings(applicationName, username, password));

    DocumentsListQuery docQuery = new DocumentsListQuery();
    docQuery.Uri = new Uri(docQuery.Uri.ToString().Replace("/default/", "/" + accountToImpersonate + "/"));

    AtomFeed docFeed = DocRequest.Service.Query(query);
    //process every document in the feed
    foreach (AtomEntry docEntry in docFeed.Entries)
    {
        //this line works for all docs (irrespective of who the author is)
        string title = docEntry.Title.Text;

        Document doc = new Document()
        {
            AtomEntry = docEntry;
        };

        //the next line throws an exception with HTTP 401 or 403 (permission) if the author is not the impersonated account
        Stream queryStream = DocRequest.Download(doc, Document.DownloadType.html)

        //do something with the stream, such as write to local disk

        //all done, close the stream
        if (queryStream != null)
            queryStream.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

模仿其他用户的正确方法是使用两条腿OAuth并将xoauth_requestor_id参数添加到uri中,以指定要为其请求数据的用户。

根据文档中的完整示例

,可以使用.NET客户端库轻松完成此操作

https://developers.google.com/gdata/docs/auth/oauth#2LeggedOAuth