1

I tried this code given below for creating Spread Sheet but got an error at this line var file = request.Execute(); how to resolve this :

    public Google.Apis.Drive.v3.Data.File CreateSheet()
    {
        string[] scopes = new string[] { DriveService.Scope.Drive,DriveService.Scope.DriveFile,}; 
        var clientId = "666641765201-qbo62utelvp1lf7iu4um406fdp6d77md.apps.googleusercontent.com";
        var clientSecret = "0Ts_Vy0-ssBLBvSTlMeu8R66";
        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 = "";
        var filename = "helloworld";
        var fileMetadata = new Google.Apis.Drive.v3.Data.File()
        {
            Name = filename,
            MimeType = "application/vnd.google-apps.spreadsheet",           
        };
        FilesResource.CreateRequest request = _service.Files.Create(fileMetadata);
        request.SupportsTeamDrives = true;
        fileMetadata.Parents = new List<string> { _parent };  
        request.Fields = "id";
        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, 
        X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
        var file = request.Execute();
        Console.WriteLine("File ID: " + file.Id);
        return file;
    }
  • Show the complete code – Atk Feb 01 '20 at 05:47
  • Dear @Atk here is the code which I used for creating google spread sheet programatically and after creation I want to export my grid data directly in google sheet. – Hemlata Singh Feb 01 '20 at 05:58
  • What is the error that you got.? – Atk Feb 01 '20 at 06:02
  • 1
    Don't show all your private client IDs and secret keys here. – Atk Feb 01 '20 at 06:14
  • Thanks for your concern Dear @Atk but these IDs are not genuine. I got Error: redirect_uri_mismatch – Hemlata Singh Feb 01 '20 at 06:44
  • Do you want to use this code for a web app, a service account or for other purpose? Have you checked the [.NET Quickstart](https://developers.google.com/sheets/api/quickstart/dotnet), it will give you a better idea on how to call the [Sheets API](https://developers.google.com/sheets/api) – alberto vielma Feb 03 '20 at 11:11
  • @albertovielma I want to use this for my web app. Yes, I go through with .Net Quickstart this is for accessing an existing sheet and I want to create new spread sheets. – Hemlata Singh Feb 04 '20 at 06:51
  • Does this answer your question? [Google OAuth 2 authorization - Error: redirect\_uri\_mismatch](https://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch), there's an explanation to your problem and how to solve it – alberto vielma Feb 04 '20 at 13:03

0 Answers0