[英]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
解决方案是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.