2

Google ドライブからファイルのリストを取得しようとしています。提供されている例をそのまま使用しています。

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

count = 0ログインしているアカウントには多くのファイルがあると確信していますが、常にファイルを返します! 他に必要なものはありますか?

編集: 認証の場合:

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

コントローラー内

  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();
   }
4

3 に答える 3

5

ファイルを取得するには、フォルダー ID を使用する必要があります。これを使用してフォルダーファイルを取得します。

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;
}
于 2013-04-30T11:48:18.153 に答える
0

ファイルとフォルダーを一覧表示できるようにするには、SCOPE を追加する必要があります。

" https://www.googleapis.com/auth/drive.appfolder " (ファイル メタデータへの読み取り専用アクセスを許可しますが、ファイル コンテンツの読み取りまたはダウンロードへのアクセスは許可しません)

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

于 2014-12-04T23:40:15.040 に答える