C# 使用.net sdk检索google drive中的文件列表
我正在尝试从Google drive获取文件列表,我正在使用它们提供的示例:C# 使用.net sdk检索google drive中的文件列表,c#,.net,asp.net-mvc-3,google-drive-api,C#,.net,Asp.net Mvc 3,Google Drive Api,我正在尝试从Google drive获取文件列表,我正在使用它们提供的示例: public static List<File> RetrieveAllFiles(DriveService service) { List<File> result = new List<File>(); FilesResource.ListRequest request = service.Files.List(); do
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;
}
内部控制器
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();
}
public ActionResult索引(字符串状态,字符串代码)
{
尝试
{
列表文件=新列表();
IAAuthenticator authenticator=Utils.GetCredentials(代码、状态);
//在会话中存储身份验证程序和授权服务
会话[“验证器”]=验证器;
DriveService=Utils.BuildService(验证器);
if(验证器!=null&&service!=null)
{
files=GoogleDriveHelper.RetrieveAllFiles(服务);
返回视图(文件);
}
}
捕获(CodeExchangeException)
{
if(会话[“服务”]==null | |会话[“验证器”]==null)
{
重定向(Utils.GetAuthorizationUrl(“,state));
}
}
捕获(NOREFRESHE异常)
{
重定向(例如授权URL);
}
返回视图();
}
您必须使用文件夹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;
}
string folderid=FindFolder(服务,根文件夹,CreatedFolder.Title);
List listadoFiles=service.Children.List(folderid.Fetch().Items.ToList();
公共静态字符串FindFolder(DriveService服务、字符串parentfolderId、字符串FolderName)
{
ChildrenResource.ListRequest=service.Children.List(parentfolderId);
request.Q=“mimeType='application/vnd.google apps.folder'和title=”+FolderName+”;
做
{
尝试
{
ChildList children=request.Fetch();
if(children!=null&&children.Items.Count>0)
{
返回子项。项[0]。Id;
}
foreach(ChildReference child在children.Items中的子项)
{
Console.WriteLine(“文件Id:+child.Id”);
}
request.PageToken=children.NextPageToken;
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
request.PageToken=null;
}
}而(!String.IsNullOrEmpty(request.PageToken));
返回字符串。空;
}
我遇到了同样的问题,并通过添加缺少的驱动器作用域解决了它。要能够列出需要添加作用域的文件和文件夹,请执行以下操作:
“”(允许对文件元数据进行只读访问,但不允许对读取或下载文件内容进行任何访问)
在这种情况下,我应该知道该驱动器中的所有文件夹?您可以获取根文件夹id,并查询它以获取所有子文件夹,但根文件夹有自己的id。您收到任何异常情况吗?如何进行授权和身份验证?我从Google sdk下载了DrEdit,使用了相同的类我刚刚更新了我的编码。请用示例说明一下好吗?我添加了以下范围:
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
。塞拉姆兄弟
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;
}