【发布时间】: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