A 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.
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.
I am actually not sure. But it shouldn’t take you to long to test the different permissions to see what you need to have. Let me know what you find out ๐
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?
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.
I would think you would need to make the file public. I haven’t really tried to do that.
When I tried to get the alternate links of the GoogleDrive files using GoogleDrive API(Service.Files.List()), I am getting different URLs as below:
Pattern 1) https://drive.google.com/drive/folders/0BwvFl02JkI0Ab3pvcS1FY293WkU.
Pattern 2) https://docs.google.com/folderview?id=0B0skzp1GyQtfZFlxY3c4SGJFd1E&usp=drivesdk
AlternateLink is not consistent?
Thanks in advance.
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.