如何使用谷歌驱动器的下载链接显示文件

时间:2014-05-29 05:18:32

标签: google-drive-api

我需要在哪里显示google驱动器中的所有文件列表(当然使用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();

        }

    }
}

0 个答案:

没有答案