使用.NET SDK检索的文件谷歌驱动器列表 [英] Retrieve list of files google drive using .net sdk
本文介绍了使用.NET SDK检索的文件谷歌驱动器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图来从谷歌驱动的文件列表,我使用他们提供的,因为它的例子是:
公共静态列表<文件> RetrieveAllFiles(DriveService服务)
{
名单<文件>结果=新名单,其中,文件>();
FilesResource.ListRequest请求= service.Files.List();
做
{
尝试
{
文件列表文件= request.Fetch();
result.AddRange(files.Items);
request.PageToken = files.NextPageToken;
}
赶上(例外五)
{
Console.WriteLine(发生错误:+ e.Message);
request.PageToken = NULL;
}
}而(String.IsNullOrEmpty(request.PageToken)!);
返回结果;
}
它总是返回文件计数= 0
而我pretty的确认登录的帐户有许多文件!是否有其他需要的东西吗?
编辑: 对于身份验证:
公共静态IAuthenticator GetCredentials(字符串授权code,字符串州)
{
字符串EMAILADDRESS =;
尝试
{
IAuthorizationState凭证=兑换code(授权code);
用户信息用户信息= GetUserInfo(凭证);
字符串userid = userInfo.Id;
EMAILADDRESS = userInfo.Email;
如果(!String.IsNullOrEmpty(credentials.RefreshToken))
{
StoreCredentials(用户标识,证书);
返回GetAuthenticatorFromState(凭证);
}
其他
{
凭据= GetStoredCredentials(用户ID);
如果(凭证= NULL和放大器;!&安培;!String.IsNullOrEmpty(credentials.RefreshToken))
{
返回GetAuthenticatorFromState(凭证);
}
}
}
赶上(codeExchangeException E)
{
Console.WriteLine(在code交换时发生错误。);
//硬盘应用应尽量以检索用户和凭据当前
//会话。
//如果没有可用的,将用户重定向到授权的URL。
e.AuthorizationUrl = GetAuthorizationUrl(EMAILADDRESS,状态);
扔ê;
}
赶上(NoUserIdException)
{
Console.WriteLine(无用户ID可以检索。);
}
//没有刷新令牌已被检索。
字符串authorizationUrl = GetAuthorizationUrl(EMAILADDRESS,状态);
抛出新NoRefreshTokenException(authorizationUrl);
}
内部静态Google.Apis.Drive.v2.DriveService编译服务(IAuthenticator凭证)
{
返回新Google.Apis.Drive.v2.DriveService(凭证);
}
在控制器
公众的ActionResult指数(字符串状态,串code)
{
尝试
{
名单<文件>文件=新的名单,其中,文件>();
IAuthenticator身份验证= Utils.GetCredentials(code,状态);
//存储认证和会话的授权服务
会话[身份验证] =身份验证;
DriveService服务= Utils.BuildService(验证);
如果(验证= NULL和放大器;!&安培;服务!= NULL)
{
文件= GoogleDriveHelper.RetrieveAllFiles(服务);
返回查看(文件);
}
}
赶上(codeExchangeException)
{
如果(会话[服务] == NULL ||会话[身份验证] == NULL)
{
的Response.Redirect(Utils.GetAuthorizationUrl(,状态));
}
}
赶上(NoRefreshTokenException E)
{
的Response.Redirect(e.AuthorizationUrl);
}
返回查看();
}
解决方案
您必须使用该文件夹id来获取文件。我用它来获取文件夹中的文件:
字符串folderid = FindFolder(服务,rootFolder,CreatedFolder.Title);
名单< ChildReference> listadoFiles = service.Children.List(folderid).Fetch()Items.ToList()。
公共静态字符串FindFolder(DriveService服务,字符串parentfolderId,字符串文件夹名称)
{
ChildrenResource.ListRequest请求= service.Children.List(parentfolderId);
request.Q =MIMETYPE ='应用程序/ vnd.google-apps.folder'和标题='+文件夹名+';
做
{
尝试
{
的childList儿童= request.Fetch();
如果(孩子= NULL和放大器;!&安培; children.Items.Count大于0)
{
返回children.Items [0] .ID;
}
的foreach(ChildReference孩子在children.Items)
{
Console.WriteLine(文件ID:+ child.Id);
}
request.PageToken = children.NextPageToken;
}
赶上(例外五)
{
Console.WriteLine(发生错误:+ e.Message);
request.PageToken = NULL;
}
}而(String.IsNullOrEmpty(request.PageToken)!);
返回的String.Empty;
}
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();
}
解决方案
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;
}
这篇关于使用.NET SDK检索的文件谷歌驱动器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文