Google drive api 如何通过google drive的下载链接显示文件

Google drive api 如何通过google drive的下载链接显示文件,google-drive-api,Google Drive Api,我有一个要求,在那里我应该显示来自google drive的所有文件的列表(当然使用oAuth进行授权和身份验证)。。。显示扩展名为(.mp3)的文件列表以及完整路径,用户可以复制文件路径并将其粘贴到单独的url中,也可以单击文件名下载文件。我没有得到从谷歌硬盘获取文件完整路径的正确输入。我正在使用asp.net应用程序 using System; using System.Collections.Generic; using System.Linq; using System.Web; usi

我有一个要求,在那里我应该显示来自google drive的所有文件的列表(当然使用oAuth进行授权和身份验证)。。。显示扩展名为(.mp3)的文件列表以及完整路径,用户可以复制文件路径并将其粘贴到单独的url中,也可以单击文件名下载文件。我没有得到从谷歌硬盘获取文件完整路径的正确输入。我正在使用asp.net应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.GData.Client;
using Google.GData.Documents;
using System.IO;
using Google.Apis.Drive.v2;
using Google.Apis.Drive;
using Google.Apis;
namespace AccessGoogleDriveData
{
    public partial class GoogleOAuthCallback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string state = Request.QueryString["state"];
            // creating oauthparameter with client id and secret key
            OAuth2Parameters parameters = new OAuth2Parameters()
            {
                ClientId = "myclientid.apps.googleusercontent.com",
                ClientSecret = "clientsecretid",
                    RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx">,
                    Scope = "https://docs.google.com/feeds/ ",
                State = "documents",
                AccessType = "offline"
            };
            lstDocuments.Visible = false;
            if (state != null)
            {
                parameters.AccessCode = Request.QueryString["code"];
                // it gets accesstoken from google
                Google.GData.Client.OAuthUtil.GetAccessToken(parameters);
                GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MyDocumentsListIntegration-v1", parameters);
                DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");
                service.RequestFactory = requestFactory;
                DocumentsListQuery query = new DocumentsListQuery();

                // Make a request to the API and get all documents.
                DocumentsFeed feed = service.Query(query);
                lstDocuments.Visible = true;

                if (feed.Entries.Count > 0)
                {
                    // var documentsList = from entry in feed.Entries select entry.Title.Text;
                    var documentsList = from entry in feed.Entries where GetFileExtension(entry.Title.Text.ToString()) == "mp3" select entry.Title.Text;

                     lstDocuments.DataSource = documentsList;
                    lstDocuments.DataBind();
                }
            }

        }

        protected void btnGetDocuments_Click(object sender, EventArgs e)
        {
            OAuth2Parameters parameters = new OAuth2Parameters()
            {
                ClientId = "myclientid.apps.googleusercontent.com",
                ClientSecret = "clientsecretid",
                    RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",
                    Scope = "https://docs.google.com/feeds/ ">,
                State = "documents",
                AccessType = "offline"   // offline means it creats a refreshtoken
            };
            string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
            Session["oauthDocumentsParameters"] = parameters;
            // it redirct to google login page
            Response.Redirect(url);
        }

        private string GetFileExtension(string sFileName)
        {
            sFileName = sFileName.Trim();

            if (String.IsNullOrEmpty(sFileName))
            {
                return String.Empty;
            }

            string sExtension = String.Empty;

            char[] cArr = sFileName.ToCharArray();

            int iIndex = 0;

            for (int i = cArr.Length - 1; i > -1; i--)
            {
                if (cArr[i].Equals('.'))
                {
                    iIndex = i;
                    break;
                }
            }

            if (iIndex > 0)
            {
                for (int i = iIndex + 1; i < cArr.Length; i++)
                {
                    sExtension += cArr[i];
                }
            }

            return sExtension.ToLower();

        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用Google.GData.Client;
使用Google.GData.Documents;
使用System.IO;
使用Google.api.Drive.v2;
使用Google.api.Drive;
使用Google.api;
命名空间访问GoogleDriveData
{
公共部分类GoogleOAuthCallback:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
string state=Request.QueryString[“state”];
//使用客户端id和密钥创建oauthparameter
OAuth2Parameters参数=新的OAuth2Parameters()
{
ClientId=“myclientid.apps.googleusercontent.com”,
ClientSecret=“clientsecretid”,
重定向URI=”http://localhost:16615/GoogleOAuthCallback.aspx">,
范围=”https://docs.google.com/feeds/ ",
State=“文件”,
AccessType=“脱机”
};
lstDocuments.Visible=false;
如果(状态!=null)
{
parameters.AccessCode=Request.QueryString[“code”];
//它从谷歌获得accesstoken
Google.GData.Client.OAuthUtil.GetAccessToken(参数);
GOAuth2RequestFactory requestFactory=新的GOAuth2RequestFactory(null,“MyDocumentsListIntegration-v1”,参数);
DocumentsService服务=新的DocumentsService(“MyDocumentsListIntegration-v1”);
service.RequestFactory=RequestFactory;
DocumentsListQuery=新建DocumentsListQuery();
//向API发出请求并获取所有文档。
DocumentsFeed feed=service.Query(查询);
lstDocuments.Visible=true;
如果(feed.Entries.Count>0)
{
//var documentsList=来自提要中的条目。条目选择entry.Title.Text;
var documentsList=来自feed.Entries中的条目,其中GetFileExtension(entry.Title.Text.ToString())==“mp3”选择entry.Title.Text;
lstDocuments.DataSource=文档列表;
lstDocuments.DataBind();
}
}
}
受保护的无效btnGetDocuments\u单击(对象发送者,事件参数e)
{
OAuth2Parameters参数=新的OAuth2Parameters()
{
ClientId=“myclientid.apps.googleusercontent.com”,
ClientSecret=“clientsecretid”,
重定向URI=”http://localhost:16615/GoogleOAuthCallback.aspx",
范围=”https://docs.google.com/feeds/ ">,
State=“文件”,
AccessType=“offline”//offline表示它创建刷新令牌
};
字符串url=Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(参数);
会话[“oauthDocumentsParameters”]=参数;
//它重新登录到谷歌登录页面
重定向(url);
}
私有字符串GetFileExtension(字符串sFileName)
{
sFileName=sFileName.Trim();
if(String.IsNullOrEmpty(sFileName))
{
返回字符串。空;
}
string sExtension=string.Empty;
char[]cArr=sFileName.ToCharArray();
国际指数=0;
对于(int i=cArr.Length-1;i>-1;i--)
{
if(cArr[i].等于('.'))
{
iIndex=i;
打破
}
}
如果(iIndex>0)
{
对于(int i=iIndex+1;i