2017-07-06 311 views
0

我需要上传我的谷歌驱动器上的文件(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; } 
} 

但我有此错误:

  1. 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 

我已经插入重定向页面,但它给了我同样的错误。

+1

当您向OAuth网站注册时,一旦该应用程序获得授权,您就可以告知其将用户重定向到哪个网页。错误消息说''http:// localhost:61360/Prova.aspx''不是你注册的那个。您在Google应用中注册的网址是什么? – Neil

+0

我不插入任何重定向页面...我只创建了一个应用程序,并在程序中使用了id和秘密。我应该把什么作为重定向网址? –

+0

也许我可以更好地解释:在“console.developers.google.com”,“凭据”中,我创建的“ID客户端OAuth 2.0”中,我只提供了名称字段。在“共识产品屏幕”中,我只有我的电子邮件地址和产品名称 –

回答

0

我已经添加了重定向URI,但是当我试图再次启动程序时,错误再次出现,告诉我授权另一个URI。我试图添加它,但每当我用另一个URI重新启动程序时都会出现错误消息。我怎样才能禁用随机端口?

相关问题