[英]ASP.NET MVC web app to upload files with Google Drive API URI mismatch
我正在嘗試使用 Google Drive API 上傳文件,並在單擊我頁面上的上傳按鈕時收到來自 Google 的 URI 不匹配錯誤。 谷歌顯示的 URI 甚至不是網站的一部分,我提供給谷歌的 URI 也不是,所以我不知道它來自哪里。
這是我根據本教程創建的 APIHelper class(這表明代碼應該可以在網站上運行)
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 的屏幕截圖。 產品和本地主機
谷歌顯示的 URI 甚至不是網站的一部分,我提供給谷歌的 URI 也不是,所以我不知道它來自哪里。
重定向 uri 是在購買您正在使用的客戶端庫時構建的。 您的應用程序設置為運行 http 而不是 https 其運行的 localhost 並且未托管,因此其 127.0.0.1 端口也由您的應用程序隨機生成或您已靜態設置。 /authorize 由客戶端庫再次附加。
重定向 uri 是您的代碼准備接受來自授權服務器的響應的位置。 此 URI 需要在 Google Cloud Console 中配置。 最簡單的解決方案是完全復制它並將其作為重定向 uri 添加到 Google 雲控制台中。 只要確保您的應用程序設置為使用 static 端口,如果端口更改將無法正常工作。
該視頻將向您展示如何添加它。 Google OAuth2:如何修復 redirect_uri_mismatch 錯誤。 第 2 部分服務器端 web 應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.