我需要在google驱动器上传文件(pdf,word,excel),如何使用C#在ASP.NET中执行此操作?你能一步一步地向我解释所有的段落吗?如果它有用,我把我的最后一个测试代码:
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; }
}
但我有这个错误:
- 这是一个错误。
错误:redirect_uri_mismatch
请求中的重定向URI http://localhost:61360/Prova.aspx, 与OAuth客户端授权的不匹配。访问 https://console.developers.google.com/apis/credentials/oauthclient/831126948299-2bopcsp3aee3harncqp5dpkq77sii3he.apps.googleusercontent.com?project=831126948299 更新授权的重定向URI。
请求详细信息:
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
我已插入重定向页面,但它给了我同样的错误。
答案 0 :(得分:0)
您在Google开发者控制台中添加的重定向uri必须与您发送http://localhost:61360的情况完全匹配,因此应在重定向URI中添加http://localhost:61360。或者可能是http://localhost:61360/AuthCallback
提示:确保将visual studio设置为不添加随机端口号。