Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Google drive sdk. How to get a list of google drive folders?

I'm writing a program to allow a user to upload files to their Google Drive account. I have the upload part working and am using OAuth2. The issue I'm currently having is getting a list of folders from the users Drive account.

I found some code that is supposed to do this using the .setUserCredentials method, but it doesn't work:

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;
        }

Nothing is returned. Ideally, I want to use OAuth2 to do this. I've been trying with the following code, trying to set the authentication token, but I always get denied access:

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;
    }

Specifically, I get "Execution of request failed: https://docs.google.com/feeds/default/private/full/-/folder - The remote server returned an error: (401) Unauthorized".

I've also tried:

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

but "State" is always null, so I get a null object error. What am I doing wrong and how is this done?

like image 216
ygetarts Avatar asked Dec 03 '25 12:12

ygetarts


1 Answers

You should use the Drive SDK, not the Documents List API, which allows you to list folders. You can use "root" as a folderId if you want to list the root directory.

like image 82
Ali Afshar Avatar answered Dec 06 '25 02:12

Ali Afshar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!