首页 > 解决方案 > 如何使用 C Sharp 创建谷歌电子表格?

问题描述

我收到这个错误

Google.GoogleApiException:'Google.Apis.Requests.RequestError
文件未找到:。[404]
调用 CreateSpreadsheet() 时出现错误 [ 消息 [找不到文件:.] 位置 [文件 ID - 参数] 原因 [未找到] 域 [全局]

(出于我的安全考虑,凭据略有不同。)

public Google.Apis.Drive.v3.Data.File CreateSheet() 
{  
    string[] scopes = new string[] { DriveService.Scope.Drive,DriveService.Scope.DriveFile,};  
    var clientId = "123456337-wajklowlksflmxiowerasjdflsl.apps.googleusercontent.com";     
    var clientSecret = "kkslfdkiwowol_ssdwerss";         
    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets       
    {  
        ClientId = clientId,  
        ClientSecret = clientSecret
    },
    scopes,  
    Environment.UserName,
    CancellationToken.None,
    new FileDataStore("MyAppsToken")).Result;  

    DriveService _service = new DriveService(new BaseClientService.Initializer()  
    {  
        HttpClientInitializer = credential,  
        ApplicationName = "MyAppName"
    });  
           
    var _parent = ""; //ID of folder if you want to create spreadsheet in specific folder
    var filename = "helloworld";
    var fileMetadata = new Google.Apis.Drive.v3.Data.File()  
    {  
        Name = filename,  
        MimeType = "application/vnd.google-apps.spreadsheet",  
        //TeamDriveId = teamDriveID, // IF you want to add to specific team drive  
    };  

    FilesResource.CreateRequest request = _service.Files.Create(fileMetadata);  
    request.SupportsTeamDrives = true;  
    fileMetadata.Parents = new List<string> { _parent }; // Parent folder id or TeamDriveID  
    request.Fields = "id";  
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };  
    var file = request.Execute();  
    MessageBox.Show("File ID: " + file.Id);  
    return file;  
}

标签: c#

解决方案


推荐阅读