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

C# 谷歌文档:无法导出/下载用户';在C中使用管理访问/模拟(禁止403)的s文档#,c#,google-docs,impersonation,C#,Google Docs,Impersonation,我整天都在为这件事绞尽脑汁,做了大量的谷歌搜索,但都没有结果。我正在编写一个应用程序,将用户的所有谷歌文档文件下载到本地磁盘。我必须为几个用户这样做,并且只提供一个管理员帐户(通过模拟使用)。该问题仅适用于模拟用户尚未编写/共享的文档。在所有情况下,获取标题信息都可以正常工作(无论作者和共享设置如何)。下载由模拟帐户编写的文件也可以正常工作。但是,下载其他用户编写的文件失败,HTTP 401或403(权限被拒绝)。有人能告诉我我做错了什么吗?下面是代码的精简版本。谢谢 using Sys

我整天都在为这件事绞尽脑汁,做了大量的谷歌搜索,但都没有结果。我正在编写一个应用程序,将用户的所有谷歌文档文件下载到本地磁盘。我必须为几个用户这样做,并且只提供一个管理员帐户(通过模拟使用)。该问题仅适用于模拟用户尚未编写/共享的文档。在所有情况下,获取标题信息都可以正常工作(无论作者和共享设置如何)。下载由模拟帐户编写的文件也可以正常工作。但是,下载其他用户编写的文件失败,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();
    }
}

模拟其他用户的正确方法是使用两条腿的OAuth,并将
xoauth\u requestor\u id
参数添加到uri中,以指定要为其请求数据的用户

根据文档中的完整示例,可以使用.NET客户端库轻松完成此操作:


克劳迪奥-非常感谢你的帮助。我能够进行身份验证,但是获取流现在返回一个异常。有什么想法吗?GDataRecitException.message=执行导致Google.GData.Client.GDataRequest.Execute()处的Google.GData.Client.GDataAuthRequest.Execute(Int32 retryCounter)处的Google.GData.Client.GDataAuthRequest.Execute()处的重定向,这是一个电子表格,我包装了DocumentService:goauthRequestFactoryRequestFactory=newgoauthRequestFactory(“writely”,applicationName);requestFactory.ConsumerKey=客户_ID;requestFactory.ConsumerCret=客户机_机密//1-初始化文档DocRequest=newdocumentsrequest(newrequestsettings(applicationName));DocRequest.Service=新文档服务(应用程序名称);DocRequest.Service.RequestFactory=RequestFactory;为了下载任何类型的文档,包括电子表格,您必须确保该应用程序被授权用于以下OAuth范围:
https://docs.google.com/feeds/
https://docs.googleusercontent.com/
https://spreadsheets.google.com/feeds/
谢谢克劳迪奥。不幸的是,我在auth参数上设置了作用域,但仍然得到重定向异常。你知道如何处理这个异常,或者我是否需要做其他事情吗?Auth 1.0似乎不支持“重定向URL”。不确定这是否是问题所在。。。谢谢您应该在控制面板中明确授权您的应用程序使用具有所需范围的2条腿OAuth。查看说明