【问题标题】:Upload file to Google Drive将文件上传到 Google 云端硬盘
【发布时间】:2017-12-10 08:44:30
【问题描述】:

我需要在我的谷歌驱动器上上传文件(pdf、word、excel),我如何在 ASP.NET 中使用 C# 来完成?你能一步一步地向我解释所有的段落吗?如果有用的话,我放上我最后的测试代码:

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. 这是一个错误。

错误: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

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

【问题讨论】:

  • 当您向 OAuth 站点注册时,您会告诉它在应用程序获得授权后将用户重定向到哪个网页。错误消息显示http://localhost:61360/Prova.aspx 不是您注册的那个。您在 Google Apps 中注册的网址是什么?
  • 我没有插入任何重定向页面...我只创建了一个应用程序并在我的程序中使用了 id 和 secret。我应该把什么作为重定向网址?
  • 也许我可以更好地解释一下:在“console.developers.google.com”中,在“凭据”中,在我创建的“ID 客户端 OAuth 2.0”中,我只输入了名称字段。在“共识产品屏幕”中,我只有我的电子邮件地址和产品名称
  • 错误消息中的链接(“访问...以更新授权的重定向 URL”)是您的朋友

标签: c# asp.net upload google-drive-api


【解决方案1】:

您在 Google 开发者控制台中添加的重定向 URI 必须与您发送的内容完全匹配,因为您发送的是 http://localhost:61360,因此应在您的重定向 URI 中添加 http://localhost:61360。或者可能是http://localhost:61360/AuthCallback

提示:确保您已将 Visual Studio 设置为不添加随机端口号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多