In my project, I want to get google drive file lists into my project. I am using ASP.NET Core.
I created a project in Google API Console successfully and implemented the code in ASP.NET core. I got Redirect URI Mismatch error every time when I run the program even I set redirect URL properly. I didn't get success.
I am using Google drive API V3.
That’s an error. Error: redirect_uri_mismatch
The redirect URI in the request, http://127.0.0.1:63354/authorize/, does not match the ones authorized for the OAuth client.
So I following below steps for my ASP.NET core project. (First I tried in console project and I got success) https://dzone.com/articles/authentication-using-google-in-aspnet-core-20
By this link, I can authenticate in google but I am not able to get files list from google drive. I am not getting any error and got files to count as zero.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WebApplication.Controllers
{
public class TestController : BaseController
{
public IActionResult Authenticate()
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "",
ClientSecret = ""
},
new[] { DriveService.Scope.Drive },
"user",
CancellationToken.None).Result;
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "WebApplication3",
});
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
ViewBag.filecount = files.Count;
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
ViewBag.fileName = file.Name;
}
}
return View("Test");
}
}
}