【问题标题】:Google drive Redirect URI Mismatch and how to get files list from google drive in ASP.net core 2.0谷歌驱动器重定向 URI 不匹配以及如何从 ASP.net 核心 2.0 中的谷歌驱动器获取文件列表
【发布时间】:2019-04-08 01:22:22
【问题描述】:

在我的项目中,我想将谷歌驱动器文件列表添加到我的项目中。我正在使用 ASP.NET Core。

我在 Google API Console 中成功创建了一个项目,并在 ASP.NET 核心中实现了代码。即使我正确设置了重定向 URL,每次运行程序时我都会收到重定向 URI 不匹配错误。我没有成功。

我正在使用 Google Drive API V3。

这是一个错误。 错误:redirect_uri_mismatch

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

所以我为我的 ASP.NET 核心项目遵循以下步骤。 (首先我在控制台项目中尝试过,我得到了成功) https://dzone.com/articles/authentication-using-google-in-aspnet-core-20

通过此链接,我可以在 google 中进行身份验证,但我无法从 google drive 获取文件列表。 我没有收到任何错误,并且文件被计为零。

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

        }
    }
}

【问题讨论】:

  • 您需要自定义 return_uri 。见stackoverflow.com/questions/31209273/…
  • 如果您没有取回任何文件,那是因为您登录的用户没有文件。尝试更改“用户”并使用其他用户登录。

标签: c# asp.net asp.net-core google-drive-api asp.net-core-2.0


【解决方案1】:

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

   listRequest.Q = 'me' in owner"

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

更多详情可以查看SO post。

【讨论】:

    【解决方案2】:

    我怀疑您的问题是使用服务帐户访问 Google 云端硬盘的常见问题,但希望看到您的用户帐户中的文件。他们是两个不同的账户。如果你搜索 SO,这个问题已经被问过很多次了。解决方案是

    1. 不要使用服务帐户,
    2. 与服务帐户共享您的文件。

    【讨论】:

    • 这个答案是正确的。您可以通过代码看到他使用的是 Web 浏览器客户端,而不是服务帐户客户端。 .net 中的服务帐户身份验证代码完全不同,而且服务帐户没有重定向 uri。
    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多