Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Upload a file to Google drive does not work!

$
0
0

Hi

I'm trying to upload a file to google drive using google drive Api in my core 2.1 project, So I followed these instructions:

https://developers.google.com/drive/api/v3/quickstart/dotnet

But it doesn't work.

The Google Drive Api settings in Google Cloud Platform:

Api is enabled:

Credentials:

Authorized redirect URI:

Of course I've copied the credentials.json json file to my project directory as mentioned in the instructions.

Orders model property (which I want to attach the file to):

public string WorkURL { get; set; }

Upload method:

        public void Upload(string path)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,"user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                //Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Drive API service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define parameters of request.
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            // List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files;


        }

OrdersController:

    public class OrdersController : Controller
    {
        private readonly ApplicationDbContext _context;
        private string[] Scopes = { DriveService.Scope.Drive };
        static string ApplicationName = "CoreWebApp";


        public OrdersController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpPost]
        public async Task<IActionResult> Edit(int id, [Bind("Id,WorkURL")] Orders orders)
        {
            if (id != orders.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Upload(orders.WorkURL);
                    _context.Update(orders);
                    await _context.SaveChangesAsync();
                    ViewBag.mess = "Saved";
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrdersExists(orders.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return View(orders);
        }

Getting the file in Edit view:

<div class="form-group"><label asp-for="WorkURL" class="control-label"></label><input asp-for="WorkURL" type="file" class="form-control" /><span asp-validation-for="WorkURL" class="text-danger"></span></div>

But when choosing some file and save the data no file is uploaded to google drive and no action happened but saving.

Why? and how to solve?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>