【发布时间】:2022-02-04 19:53:09
【问题描述】:
我正在尝试使用 Google Drive API 上传文件,并在单击我页面上的上传按钮时收到来自 Google 的 URI 不匹配错误。 Google 显示的 URI 甚至不是网站的一部分,我提供给 Google 的 URI 也不是,所以我不知道它来自哪里。
这是我基于 this tutorial 创建的 APIHelper 类(这表明代码应该可以在网站上运行)
public class GoogleDriveAPIHelper
{
//add scope
public static string[] Scopes = { DriveService.Scope.Drive };
//create Drive API service.
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
//Root Folder of project
var CSPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))
{
string FolderPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
string FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
//create Drive API service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Documents Uploader",
});
return service;
}
//file Upload to the Google Drive.
public static void UploadFile(string folderID, HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
//create service
DriveService service = GetService();
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
var FileMetaData = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(file.FileName),
MimeType = MimeMapping.GetMimeMapping(path),
//id of parent folder
Parents = new List<string>
{
folderID
}
};
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(path, FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}
}
}
}
还有帖子
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string folderID = "1L9QUUgmtg8KUdNvutQ1yncIwN_uLz4xs";
if (TempData["Success"] == null)
{
// show all fields
ViewBag.ShowForm = true;
ViewBag.ShowButtons = false;
}
else
{
// hide all elements on the page for success message
ViewBag.ShowForm = false;
ViewBag.ShowButtons = true;
}
GoogleDriveAPIHelper.UploadFile(folderID, file);
TempData["Success"] = "File successfully uploaded";
return View();
}
我听说教程引用的代码仅适用于独立应用程序而不适用于 Web 应用程序,因此教程中的屏幕截图来自网站很奇怪。 耸耸肩 我会继续寻找技巧和窍门,但与此同时,我发布此内容是为了查看是否有其他人编写了一个网站以通过 Google 驱动器上传到特定文件夹,而不是根。蒂亚!
编辑:以下是我在 Google Cloud Console 中设置的重定向 URI 的屏幕截图。产品和本地主机
编辑:Startup.Auth.cs - 用于通过 ADFS 身份验证,与 Google Drive API 无关
private void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
// TempData and Owin don't get along, use this workaround to force a custom cookie manager
// https://stackoverflow.com/questions/28559237/intermittent-redirection-loops-during-adfs-authentication
CookieManager = new SystemWebCookieManager()
});
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = ConfigurationManager.AppSettings["ida:Wtrealm"],
MetadataAddress = ConfigurationManager.AppSettings["ida:ADFSMetadata"]
});
}
领域与 Google 控制台中的 URI 相匹配,元数据与我在所有使用 ADFS 的 Web 应用程序中使用的 xml 链接相同,通过身份验证,它可以完美运行。我的 web.config 文件中也没有提到 Google 所说的 IP 地址是我的重定向 URI。
【问题讨论】:
-
首先您的应用是否在谷歌云控制台中投入生产?其次,您可以发布您设置的重定向 uri 的图片吗?
-
是的,它在 GCC 中设置为生产,我将添加我设置的重定向 URI 的屏幕截图。
-
您也可以发布您的身份验证代码吗?如果您使用 Javascript,则只需添加 Javascript 源。
-
我添加了我的 Startup.Auth 类,感谢您的关注
-
我用代码走了一条不同的路线,但仍然有一个 URI 不匹配,只是这次有点不同。我会发布这个问题,因为我现在被这个问题困住了。
标签: c# asp.net-mvc google-drive-api