C# 谷歌驱动器 SDK。如何获取谷歌驱动器文件夹列表?

标签 c# google-drive-api

我正在编写一个程序,允许用户将文件上传到他们的 Google 云端硬盘帐户。我有上传部分工作并且正在使用 OAuth2。我目前遇到的问题是从用户云端硬盘帐户获取文件夹列表。

我找到了一些应该使用 .setUserCredentials 方法执行此操作的代码,但它不起作用:

DocumentsService service1 = new DocumentsService("project");            
        service1.setUserCredentials("user","pass");

        FolderQuery query1 = new FolderQuery();

        // Make a request to the API and get all documents.
        DocumentsFeed feed = service1.Query(query1);

        // Iterate through all of the documents returned
        foreach (DocumentEntry entry in feed.Entries)
        {
            var blech = entry.Title.Text;
        }

什么都没有返回。理想情况下,我想使用 OAuth2 来执行此操作。我一直在尝试使用以下代码,尝试设置身份验证 token ,但我总是被拒绝访问:

String CLIENT_ID = "clientid";
String CLIENT_SECRET = "secretid";

var docprovider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var docstate = GetDocAuthentication(docprovider);

DocumentsService service1 = new DocumentsService("project");

service1.SetAuthenticationToken(docstate.RefreshToken);
FolderQuery query1 = new FolderQuery();

DocumentsFeed feed = service1.Query(query1); //get error here

        // Iterate through all of the documents returned
        foreach (DocumentEntry entry in feed.Entries)
        {
            // Print the title of this document to the screen
            var blech = entry.Title.Text;
        }

..

    private static IAuthorizationState GetDocAuthentication(NativeApplicationClient client)
    {
        const string STORAGE = "storagestring";
        const string KEY = "keystring";
        string scope = "https://docs.google.com/feeds/default/private/full/-/folder";            
        // Check if there is a cached refresh token available.
        IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
        if (state != null)
        {
            try
            {
                client.RefreshToken(state);
                return state; // Yes - we are done.
            }
            catch (DotNetOpenAuth.Messaging.ProtocolException ex)
            {

            }
        }

        // Retrieve the authorization from the user.
        state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
        AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
        return state;
    }

具体来说,我收到“请求执行失败:https://docs.google.com/feeds/default/private/full/-/folder - 远程服务器返回错误:(401) 未经授权”。

我也试过:

var docauth = new OAuth2Authenticator<NativeApplicationClient>(docprovider, GetDocAuthentication);
DocumentsService service1 = new DocumentsService("project");
service1.SetAuthenticationToken(docauth.State.AccessToken);

但是“State”总是空的,所以我得到一个空对象错误。我做错了什么,这是怎么做到的?

最佳答案

你应该使用 Drive SDK ,而不是文档列表 API,它允许您 list folders .如果要列出根目录,可以使用“root”作为 folderId。

关于C# 谷歌驱动器 SDK。如何获取谷歌驱动器文件夹列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12272920/

相关文章:

google-sheets - 为 Google 电子表格添加/创建列的日期

ios - 在 Xcode 6.1 中使用 Google API Objective-C 客户端导入 Google Drive SDK 时出错

c# - 驱动器号与 DevicePath

google-apps-script - 通过 Google Apps 脚本发布 Google 电子表格

google-drive-api - Google Drive API 403 禁止访问

c# - 在C#中的rabbit Mq中获取xDeath中队列消息的最大重试次数

javascript - 观看/收听对 Google 云端硬盘电子表格的更改

c# - 将 winforms 控件的属性绑定(bind)到多个对象属性

c# - WPF C# 在应用程序启动时隐藏 TabControl 项

c# - .NET 日期时间到字符串