简体   繁体   English

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

[英]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. 在我的项目中,我希望将google驱动器文件列表添加到我的项目中。 I am using ASP.NET Core. 我正在使用ASP.NET Core。

I created a project in Google API Console successfully and implemented the code in ASP.NET core. 我成功地在Google API控制台中创建了一个项目,并在ASP.NET核心中实现了该代码。 I got Redirect URI Mismatch error every time when I run the program even I set redirect URL properly. 每次运行程序时都会出现Redirect URI Mismatch错误,即使我正确设置了重定向URL。 I didn't get success. 我没有成功。

I am using Google drive API V3. 我正在使用Google drive API V3。

That's an error. 那是一个错误。 Error: redirect_uri_mismatch 错误: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. 请求中的重定向URI( http://127.0.0.1:63354/authorize/)与授权给OAuth客户端的URI不匹配。

So I following below steps for my ASP.NET core project. 所以我按照以下步骤进行ASP.NET核心项目。 (First I tried in console project and I got success) https://dzone.com/articles/authentication-using-google-in-aspnet-core-20 (首先我尝试在控制台项目中取得成功) 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传递一个参数,如下所示:

   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 . 有关详细信息,您可以查看此SO帖子

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. 我怀疑您的问题通常是使用服务帐户访问Google云端硬盘,但希望看到您的用户帐户中的文件。 They are two different accounts. 他们是两个不同的帐户。 If you search SO, this question has been asked many times. 如果您搜索SO,则会多次询问此问题。 The solutions are 解决方案是

  1. don't use a Service Account, 不要使用服务帐户,
  2. share your files with the Service Account. 与服务帐户共享您的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC web 应用程序使用 Google Drive 上传文件 API URI 不匹配 - ASP.NET MVC web app to upload files with Google Drive API URI mismatch Asp.net和Google开发者控制台-重定向URI不匹配 - Asp.net and Google developer console — redirect URI mismatch 如何将文件从Google驱动器同步到我的asp.net Web应用程序? - How to sync files from google drive to my asp.net web application? Google云端硬盘OAuth给出错误:浏览器上的redirect_uri_mismatch - Google Drive OAuth giving Error: redirect_uri_mismatch on browser 使用C#在ASP.Net中使用Google Drive API将文件上传到Google云端硬盘 - Upload Files to Google Drive using Google Drive API in ASP.Net with C# 通过asp.net核心代码从谷歌驱动器下载的Excel表格格式不正确 - Excel sheet downloaded from google drive via asp.net core code is not in correct format 在ASP.NET中上传到Google驱动器 - Upload to google drive in ASP.NET 使用 asp.net mvc 从 Google Drive Api 下载文件的问题 - Problems with files downloading from Google Drive Api using asp.net mvc 带有谷歌驱动器 API OAuth 2.0 的 Asp.net MVC 在生产中不起作用 - Asp.net MVC with google drive API OAuth 2.0 not working in production 如果Asp.net的Google云端硬盘中不存在该文件夹,则创建该文件夹 - Create folder if it does not exist in the Google Drive in Asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM