使用 .NET API 在 google 驱动器中看不到新电子表格
new spreadsheet not visible in google drive using .NET API
我使用 Google API NET 创建了一个 google 电子表格,如下所示。我没有收到任何异常,并且从代码中我也能够检索文件 ID。但是当我查看 google 驱动器时,我没有看到文件。我应该做些什么来通知 google 驱动器。我也刷新了我的驱动器。但是没有用。有什么想法吗?
我如下验证了我的服务:
authenticate.cs
public class SerivceAccount
{
private const string ServiceAccountEmail = "182464555438-@developer.gserviceaccount.com";
private static readonly X509Certificate2 Certificate = new X509Certificate2("ADExpress.p12", "notasecret", X509KeyStorageFlags.Exportable);
readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[]
{
"https://spreadsheets.google.com/feeds",
DriveService.Scope.Drive,
"https://www.googleapis.com/auth/drive.file"
}
}.FromCertificate(Certificate));
public bool Authenticate()
{
var isAuthenticated = false;
try
{
if (!_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
throw new InvalidOperationException("Access token request failed.");
isAuthenticated = true;
}
catch (Exception e)
{
Console.WriteLine("Exception in Authenticating " + e.ToString());
}
return isAuthenticated;
}
public ServiceAccountCredential Credential
{
get { return _credential; }
}
}
main.cs
var account = new SerivceAccount();
if (account.Authenticate())
{
FilesResource.InsertRequest request = service.Files.Insert(new Google.Apis.Drive.v2.Data.File()
{
Title = "Test",
Description = "Test",
MimeType = "application/vnd.google-apps.spreadsheet"
});
var file = request.Execute();
Console.WriteLine("File ID: " + file.Id);
}
实际上这有助于解决我的问题。我需要提供文件权限才能在 UI 界面中显示。希望这有帮助。
Permission newPermission = new Permission();
newPermission.Value = "yourdriveaccount@domain.com";
newPermission.Type = "user";
newPermission.Role = "reader";
service.Permissions.Insert(newPermission, file.Id).Execute();
您正在使用服务帐户进行身份验证。
I did a refresh my drive too
检查您自己的 Google Drive web 版本不会向您显示由服务帐户创建的文件。服务帐户是它自己的用户。您的文件可能是在服务帐户 Google Drive 帐户上创建的,您可以通过 files.list
进行测试
示例:
/// <summary>
/// Retrieve a list of File resources.
/// </summary>
/// <param name="service">Drive API service instance.</param>
/// <returns>List of File resources.</returns>
public static List<File> retrieveAllFiles(DriveService service) {
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
do {
try {
FileList files = request.Execute();
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;
}
为了让其他用户看到该文件,您必须授予他们查看由服务帐户创建的文件的权限。
我使用 Google API NET 创建了一个 google 电子表格,如下所示。我没有收到任何异常,并且从代码中我也能够检索文件 ID。但是当我查看 google 驱动器时,我没有看到文件。我应该做些什么来通知 google 驱动器。我也刷新了我的驱动器。但是没有用。有什么想法吗?
我如下验证了我的服务:
authenticate.cs
public class SerivceAccount
{
private const string ServiceAccountEmail = "182464555438-@developer.gserviceaccount.com";
private static readonly X509Certificate2 Certificate = new X509Certificate2("ADExpress.p12", "notasecret", X509KeyStorageFlags.Exportable);
readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[]
{
"https://spreadsheets.google.com/feeds",
DriveService.Scope.Drive,
"https://www.googleapis.com/auth/drive.file"
}
}.FromCertificate(Certificate));
public bool Authenticate()
{
var isAuthenticated = false;
try
{
if (!_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
throw new InvalidOperationException("Access token request failed.");
isAuthenticated = true;
}
catch (Exception e)
{
Console.WriteLine("Exception in Authenticating " + e.ToString());
}
return isAuthenticated;
}
public ServiceAccountCredential Credential
{
get { return _credential; }
}
}
main.cs
var account = new SerivceAccount();
if (account.Authenticate())
{
FilesResource.InsertRequest request = service.Files.Insert(new Google.Apis.Drive.v2.Data.File()
{
Title = "Test",
Description = "Test",
MimeType = "application/vnd.google-apps.spreadsheet"
});
var file = request.Execute();
Console.WriteLine("File ID: " + file.Id);
}
实际上这有助于解决我的问题。我需要提供文件权限才能在 UI 界面中显示。希望这有帮助。
Permission newPermission = new Permission();
newPermission.Value = "yourdriveaccount@domain.com";
newPermission.Type = "user";
newPermission.Role = "reader";
service.Permissions.Insert(newPermission, file.Id).Execute();
您正在使用服务帐户进行身份验证。
I did a refresh my drive too
检查您自己的 Google Drive web 版本不会向您显示由服务帐户创建的文件。服务帐户是它自己的用户。您的文件可能是在服务帐户 Google Drive 帐户上创建的,您可以通过 files.list
进行测试示例:
/// <summary>
/// Retrieve a list of File resources.
/// </summary>
/// <param name="service">Drive API service instance.</param>
/// <returns>List of File resources.</returns>
public static List<File> retrieveAllFiles(DriveService service) {
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
do {
try {
FileList files = request.Execute();
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;
}
为了让其他用户看到该文件,您必须授予他们查看由服务帐户创建的文件的权限。