Click here to Skip to main content
15,895,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code in C# that inserts a file into Google Drive and them displays file ID and shareable link amongst other information.
I need to get file metadata using the file iD, I have the code that is Javascript, but I do not know how to use it
Javascript Code
<pre lang="HTML"><pre><script type="text/javascript">

        function printFile(fileId) {
            var theID = '0B2XoJnBPsMpeaTdrX2pibl9GOE0';
            var request = gapi.client.request({
                'path': '/drive/v2/files/' + theID,
                'method': 'GET',
            });
            request.execute(function (resp) {
                console.log('Title: ' + resp.title);
                console.log('Description: ' + resp.description);
                console.log('MIME type: ' + resp.mimeType);
                console.log('WebContent: ' + resp.webContentLink);
            });
        }

    </script>


What I have tried:

C#
using Google.Apis.Drive.v3.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

using System.Net;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GoogleConnect.ClientId = "----------------";
        GoogleConnect.ClientSecret = "------------";
        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;
            Label1.Text = file.WebContentLink;
           
            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; }
    }
    
    }

}
Posted
Updated 2-May-17 21:50pm
v2
Comments
Member 13122739 6-May-17 2:17am    
Okay, I will run it, my code does contain Google API,
using Google.Apis.Drive.v3.Data;
iT seems the Javascript does not load, and therefore the metadata doesnt show

1 solution

Use the google drive api for that.
Google Drive Api[^]

There is a good how to implement tutorial on the site and a sample project.
Just implement it that way and you will be good to go.
 
Share this answer
 
Comments
Member 13122739 5-May-17 2:22am    
Hi, thanks for your reply, I actually did use it, but it didnt work, I am a beginner at javascript, my main programming language is asp.net c#
Wessel Beulink 5-May-17 3:23am    
Can you update your solution with the google drive api and tell me what is not working properly?
Member 13122739 6-May-17 3:51am    
I have run the code and it does have a google drive API, it does store my files in GDrive, I think the problem is with the javascript it doesnt run the function and display the metadata
string fileId = "0B2XoJnBPsMpeaTdrX2pibl9GOE0";
ClientScript.RegisterStartupScript(this.GetType(), "fileId", "updateProgress('" + fileId + "');", true);
//string resutOfExecuteJavaScript = hdnVal.Value;
Label3.Text = hdnResultValue.Value;

this is the code i used for it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900