2

I'm trying to fetch list of files from Google drive, I'm using the example they provide as it is:

public static List<File> RetrieveAllFiles(DriveService service)
    {
        List<File> result = new List<File>();
        FilesResource.ListRequest request = service.Files.List();

        do
        {
            try
            {
                FileList files = request.Fetch();

                result.AddRange(files.Items);
                request.PageToken = files.NextPageToken;
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                request.PageToken = null;
            }
        } while (!String.IsNullOrEmpty(request.PageToken));
        return result;
    }

It always returns files count = 0 while I'm pretty sure the logged in account has many files! Is there something else required?

Edit: For authentication :

  public static IAuthenticator GetCredentials(String authorizationCode, String state)
    {
        String emailAddress = "";
        try
        {
            IAuthorizationState credentials = ExchangeCode(authorizationCode);
            Userinfo userInfo = GetUserInfo(credentials);
            String userId = userInfo.Id;
            emailAddress = userInfo.Email;
            if (!String.IsNullOrEmpty(credentials.RefreshToken))
            {
                StoreCredentials(userId, credentials);
                return GetAuthenticatorFromState(credentials);
            }
            else
            {
                credentials = GetStoredCredentials(userId);
                if (credentials != null && !String.IsNullOrEmpty(credentials.RefreshToken))
                {
                    return GetAuthenticatorFromState(credentials);
                }
            }
        }
        catch (CodeExchangeException e)
        {
            Console.WriteLine("An error occurred during code exchange.");
            // Drive apps should try to retrieve the user and credentials for the current
            // session.
            // If none is available, redirect the user to the authorization URL.
            e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);
            throw e;
        }
        catch (NoUserIdException)
        {
            Console.WriteLine("No user ID could be retrieved.");
        }
        // No refresh token has been retrieved.
        String authorizationUrl = GetAuthorizationUrl(emailAddress, state);
        throw new NoRefreshTokenException(authorizationUrl);
    }

     internal static Google.Apis.Drive.v2.DriveService BuildService(IAuthenticator credentials)
    {
        return new Google.Apis.Drive.v2.DriveService(credentials);
    }

In Controller

  public ActionResult Index(string state, string code)
    {
        try
        {
            List<File> files = new List<File>();
            IAuthenticator authenticator = Utils.GetCredentials(code, state);
            // Store the authenticator and the authorized service in session
            Session["authenticator"] = authenticator;
            DriveService service = Utils.BuildService(authenticator);

            if (authenticator != null && service != null)
            {
                files = GoogleDriveHelper.RetrieveAllFiles(service);
                return View(files);
            }
        }
        catch (CodeExchangeException)
        {
            if (Session["service"] == null || Session["authenticator"] == null)
            {
                Response.Redirect(Utils.GetAuthorizationUrl("", state));
            }
        }
        catch (NoRefreshTokenException e)
        {
            Response.Redirect(e.AuthorizationUrl);
        }
     return View();
   }
svidgen
  • 13,744
  • 4
  • 33
  • 58
Mohamed Farrag
  • 1,682
  • 2
  • 19
  • 41

3 Answers3

5

You have to use the folder id to get files. I use this to get folder files:

string folderid = FindFolder(service, rootFolder,CreatedFolder.Title);
List<ChildReference> listadoFiles = service.Children.List(folderid).Fetch().Items.ToList();

public static string FindFolder(DriveService service,String parentfolderId, string FolderName)
{
    ChildrenResource.ListRequest request = service.Children.List(parentfolderId);
    request.Q = "mimeType='application/vnd.google-apps.folder' and title='" + FolderName + "' ";
    do
    {
        try
        {
            ChildList children = request.Fetch();

            if (children != null && children.Items.Count > 0)
            {

                return children.Items[0].Id;
            }

            foreach (ChildReference child in children.Items)
            {
                Console.WriteLine("File Id: " + child.Id);
            }
            request.PageToken = children.NextPageToken;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
            request.PageToken = null;
        }
    } while (!String.IsNullOrEmpty(request.PageToken));

    return string.Empty;
}
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
0

I encountered the same issue and solved it by adding missing drive scopes.

Edi
  • 109
  • 11
  • could you please clarify a little with example ? – Mohamed Farrag Nov 28 '13 at 00:23
  • I added the following scopes: `https://www.googleapis.com/auth/drive`, `https://www.googleapis.com/auth/drive.appdata`, `https://www.googleapis.com/auth/drive.file`, `https://www.googleapis.com/auth/drive.metadata.readonly`, `https://www.googleapis.com/auth/drive.readonly`. Selamu alejkum brother – Edi Nov 29 '13 at 13:53
0

To be able to list files and folders you need add the SCOPE:

"https://www.googleapis.com/auth/drive.appfolder" (Allows read-only access to file metadata, but does not allow any access to read or download file content)

https://developers.google.com/drive/web/scopes

Fernando JS
  • 4,267
  • 3
  • 31
  • 29