Google Drive API Link to file 7


Please Share

google driveA reader (Andreas) recently posted a comment on the Google Drive API Upload with C# tutorial.   It was a very good question so I thought we would take a quick look at it.

Andreas comment was :

I really like the example. But after upload I want to share the link to the file?

Prerequisites

This post assumes that you have already read the tutorial about Google Drive API with C# .net – Authentication. I will not be going into how to create a valid Drive service if you haven’t created one already please go read that tutorial then come back here.

Andreas was had probably just uploaded a file.   Google drive API file.insert returns a file resource when the file has been uploaded.    File.list returns a list of file resources so I think we can safely use that as our example here.

The Code

The first thing I am going to do is get a list of all the files on my Google Drive account using the driveService I created from my authentication flow.

 var service = service.Files.List().Execute();

 foreach(var item in result.Items){
       Console.WriteLine("Alternate link: " + item.AlternateLink);            
    }

I am going to loop though each of the items. Which if you remember correctly are just file resources.   The field we are looking for is called alternateLink

After consulting the documentation we see that

alternateLink string A link for opening the file in a relevant Google editor or viewer.

If you give another user the alternateLink to the file on Google drive they should be able to either open it on Google drive or download it.

Conclusion

When we upload a file to Google drive using File.insert a file resource is returned to us if the upload was successful. We can find the url of the file on Google drive in the alternateLink field of the file resource.

Giving this link to the file resource should allow anyone to open or download the file. I suspect this will depend upon the permissions of the file. Has anyone had any issues using this alternateLink? Please leave a comment below if you have.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.


Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “Google Drive API Link to file

  • Anchal Jain

    Hey Linda, this is an amazing tutorial, i tried to send the alternative link of a file to a friend in order to share the file , but it says that “You need permission” , so could you please tell me a way to give permission to a particular file , so that anyone with that link could view and download it.

    Thanks.

  • landsraad59

    Hi Linda,
    nice tutorial. it helped me a lot. 😉

    for me, alternate link is empty. So I proceeded in a different way.
    I set up a URL as follows:
    strAddress string = “https://drive.google.com/uc?export=download&id=” + File.Id;

    but it is not perfect. After the upload, if I want to download the file from this URL using Webclient, the file is not complete.

    I found it necessary to go click on “get the downloadable link” in Google Drive. After that click without further action, the download works. this should probably enable sharing in Google Drive.

    I would like to reproduce this action of “sharing” in my C# code but my internet research is inconclusive.

    is this tell you something?

  • umar memon

    What permission i need to give to a file stored on service account that any user ca view and edit that file.
    Using this AlternateLink.

    • Linda Lawton
      Linda Lawton Post author

      No its not constant and no i haven’t figured out why its not contestant. I wondered if it was due to the type of file. Or maybe google has a different API serving files on different servers. Depending upon where your file lives you may get a different format.