C# 如何通过api检索在google drive web界面中创建的文件和文件夹的详细信息?
如果我在google drive web界面中创建了一个新文件夹,则不会通过调用c#中的api来检索元数据 但是,如果我通过api上传文件或创建文件夹,元数据将按预期返回 问题与此相反(几乎),用户无法看到通过api上传的web界面中的文件。。。 所有文件都可以在GoogleDrive的web界面中看到,但我只能在下面的代码中获得通过api创建的文件的详细信息 (编辑)我应该补充一点,我已经尝试使用了所有可用的“DriveService.Scope” (edit2)我观察到,如果更改ApplicationName,也不会检索以前上载的文件,但会检索新上载的文件C# 如何通过api检索在google drive web界面中创建的文件和文件夹的详细信息?,c#,google-api,google-drive-api,google-api-dotnet-client,C#,Google Api,Google Drive Api,Google Api Dotnet Client,如果我在google drive web界面中创建了一个新文件夹,则不会通过调用c#中的api来检索元数据 但是,如果我通过api上传文件或创建文件夹,元数据将按预期返回 问题与此相反(几乎),用户无法看到通过api上传的web界面中的文件。。。 所有文件都可以在GoogleDrive的web界面中看到,但我只能在下面的代码中获得通过api创建的文件的详细信息 (编辑)我应该补充一点,我已经尝试使用了所有可用的“DriveService.Scope” (edit2)我观察到,如果更改Appli
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name, size, mimeType)";
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
return files;
GetCredential方法
private UserCredential GetCredential()
{
UserCredential credential;
try
{
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gd-cred-" + ApplicationName + ".json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
return credential;
}
catch (Exception ex)
{
error = ex as Error;
return null;
}
}
您使用的是Oauth2还是服务帐户?我使用的是Oauth2.com,您正在使用访问web界面时使用的同一用户对代码进行身份验证?您在该请求中仅请求了10个文件,您是否也实现了分页以获得下一组结果?如果您包含身份验证代码,可能会有所帮助。这听起来像是权限问题。您使用API的代码似乎与您在web应用程序上使用的不是同一个用户。它肯定是同一个用户,当我登录google并转到drive时,我可以看到我通过c#API上传的文件。当我很快回来时,我将添加身份验证代码,它直接来自api参考。
private UserCredential GetCredential()
{
UserCredential credential;
try
{
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gd-cred-" + ApplicationName + ".json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
return credential;
}
catch (Exception ex)
{
error = ex as Error;
return null;
}
}