新的电子表格不可见的谷歌驱动器使用.NET API



我使用google API NET创建了一个google电子表格,如下所示。我没有收到任何异常,从代码中我也能够检索到文件id。但当我在谷歌驱动器中查看时,我并没有看到文件。有什么事情我应该通知谷歌驱动器。我也刷新了我的硬盘。但没有用。知道吗?

我验证了我的服务,如下所示:

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

您正在使用服务帐户进行身份验证。

我也刷新了我的驱动器

检查您自己的Google Drive网络版本不会显示由服务帐户创建的文件。服务帐户是它自己的用户。你的文件可能是在服务帐户谷歌驱动器帐户上创建的,你可以通过做文件来测试。列出

示例:

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

为了让其他用户查看该文件,您必须授予他们查看服务帐户创建的文件的权限。

实际上这有助于解决我的问题。我需要为文件提供在UI界面中可见的权限。希望这能有所帮助。

Permission newPermission = new Permission();
newPermission.Value = "yourdriveaccount@domain.com";
newPermission.Type = "user";
newPermission.Role = "reader";
service.Permissions.Insert(newPermission, file.Id).Execute();

相关内容

  • 没有找到相关文章

最新更新