我需要上傳我的谷歌驅動器上的文件(pdf,word,excel),如何在ASP.NET中使用C#做到這一點?你能否一步一步解釋我所有的經文?如果它是有用的,我把我最後的測試代碼:上傳文件到Google Drive
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPSnippets.GoogleAPI;
using System.Runtime.Serialization;
using System.IO;
using System.Web.Script.Serialization;
...
protected void Page_Load(object sender, EventArgs e)
{
GoogleConnect.ClientId = "my_client_id";
GoogleConnect.ClientSecret = "my_client_secret";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Drive;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
{
string code = Request.QueryString["code"];
string json = GoogleConnect.PostFile(code, (HttpPostedFile)Session["File"], Session["Description"].ToString());
GoogleDriveFile file = (new JavaScriptSerializer()).Deserialize<GoogleDriveFile>(json);
tblFileDetails.Visible = true;
lblTitle.Text = file.Title;
lblId.Text = file.Id;
imgIcon.ImageUrl = file.IconLink;
lblCreatedDate.Text = file.CreatedDate.ToString();
lnkDownload.NavigateUrl = file.WebContentLink;
if (!string.IsNullOrEmpty(file.ThumbnailLink))
{
rowThumbnail.Visible = true;
imgThumbnail.ImageUrl = file.ThumbnailLink;
}
}
if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
}
protected void UploadFile(object sender, EventArgs e)
{
Session["File"] = FileUpload1.PostedFile;
Session["Description"] = txtDescription.Text;
GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}
}
public class GoogleDriveFile
{
public string Id { get; set; }
public string Title { get; set; }
public string OriginalFilename { get; set; }
public string ThumbnailLink { get; set; }
public string IconLink { get; set; }
public string WebContentLink { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
}
但我有此錯誤:
- That’s an error.
Error: redirect_uri_mismatch
The redirect URI in the request, http://localhost:61360/Prova.aspx , does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/831126948299-2bopcsp3aee3harncqp5dpkq77sii3he.apps.googleusercontent.com?project=831126948299 to update the authorized redirect URIs.
請求細節:
scope=https://www.googleapis.com/auth/drive.file redirect_uri=http://localhost:61360/Prova.aspx response_type=code client_id=831126948299-2bopcsp3aee3harncqp5dpkq77sii3he.apps.googleusercontent.com approval_prompt=auto access_type=offline
我已經插入重定向頁面,但它給了我同樣的錯誤。
當您向OAuth網站註冊時,一旦該應用程序獲得授權,您就可以告知其將用戶重定向到哪個網頁。錯誤消息說''http:// localhost:61360/Prova.aspx''不是你註冊的那個。您在Google應用中註冊的網址是什麼? – Neil
我不插入任何重定向頁面...我只創建了一個應用程序,並在程序中使用了id和祕密。我應該把什麼作爲重定向網址? –
也許我可以更好地解釋:在「console.developers.google.com」,「憑據」中,我創建的「ID客戶端OAuth 2.0」中,我只提供了名稱字段。在「共識產品屏幕」中,我只有我的電子郵件地址和產品名稱 –