简体   繁体   中英

Google drive Redirect URI Mismatch and how to get files list from google drive in ASP.net core 2.0

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

        }
    }
}

I think what you're missing here is to set the owner of the drive you want to access. You can pass a parameter in the listRequest like this:

   listRequest.Q = 'me' in owner"

you can add "me" as the owner of the drive you want to access.

For more details, you can check this SO post .

I suspect that your problem is the usual one of using a Service Account to access Google Drive, but expecting to see the files from your User Account. They are two different accounts. If you search SO, this question has been asked many times. The solutions are

  1. don't use a Service Account,
  2. share your files with the Service Account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM