谷歌驱动器重定向URI不匹配以及如何从ASP.net核心2.0中的谷歌驱动器获取文件列表

Roh*_*yas 6 c# asp.net google-drive-api asp.net-core asp.net-core-2.0

在我的项目中,我希望将google驱动器文件列表添加到我的项目中.我正在使用ASP.NET Core.

我成功地在Google API控制台中创建了一个项目,并在ASP.NET核心中实现了代码.每次运行程序时都会出现Redirect URI Mismatch错误,即使我正确设置了重定向URL.我没有成功.

我正在使用Google drive API V3.

那是一个错误.错误:redirect_uri_mismatch

请求中的重定向URI(http://127.0.0.1:63354/authorize/)与授权给OAuth客户端的URI 不匹配.

所以我按照以下步骤进行ASP.NET核心项目.(首先我尝试在控制台项目中取得成功) https://dzone.com/articles/authentication-using-google-in-aspnet-core-20

通过这个链接,我可以在谷歌验证,但我无法从谷歌驱动器获取文件列表.我没有收到任何错误,并将文件计为零.

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

jes*_*ess 0

我认为您在这里缺少的是设置您想要访问的驱动器的所有者。您可以像这样传递参数listRequest

   listRequest.Q = 'me' in owner"
Run Code Online (Sandbox Code Playgroud)

您可以将“我”添加为您要访问的驱动器的所有者。

有关更多详细信息,您可以查看此SO 帖子