是否可以从C#中的GDrive API获取带有共享选项的下载链接? [英] Is it possible to get the download link with sharing option from GDrive API in C#?

查看:114
本文介绍了是否可以从C#中的GDrive API获取带有共享选项的下载链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件上传到GDrive,并为所有具有链接的用户获取下载链接.目前,我设法上传文件并获得

I want to upload files to GDrive and get the download link for all users that have the link. Currently I managed to upload files and get the download link. But I don't know how to get the download link for all users. Can anyone help me with this, please?

推荐答案

您必须编辑文件的权限,以使所有用户都可以访问该文件.

You have to edit the permission of the file to make it accessble for all user.

  • I have done the python version. C# will have the same implementation.
  • https://developers.google.com/sheets/api/quickstart/python
  • Use the above link to create a project and download client_secret.json.
  • Create a python file in the same location where the client_secret.json is located.
  • Then use the code below to upload and create sharable link.

代码:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import multiprocessing as mp
import os
gauth = GoogleAuth()
# Creates local webserver and auto handles authentication.
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
#if there is any error in clent_secret.json try downloading it again after enabling drive api
#now the drive is accessible through python

#Lets create a folder named 'New_folder' in gdrive
file1 = drive.CreateFile({'title'   : 'New_folder',
                          'mimeType': "application/vnd.google-apps.folder"})
file1.Upload()
#it will create a folder in your drive 
#mimeType is an argument which specifies the type of file

#Now we have created a folder, lets see how to upload a file to the folder which we have created

#We need to obtain the Id of the folder which we have created in the previous steps 
folder_id = file1['id']

#lets upload an image to the folder
file2 = drive.CreateFile({'title':'filename.jpg',
                          'mimeType':'image/jpeg',
                          'parents': [{"kind": "drive#fileLink", "id": folder_id}]
#In parents argument above we need to specify the folder ID of the folder to which it has to be uploaded.
#mimeType is different for each file type, which are available in the google api documentation.

#specify the local path with the quotes in the below line
file2.SetContentFile('/home/username/Downloads/image.jpg')
file2.Upload()

#SET PERMISSION
permission = file2.InsertPermission({
                            'type': 'anyone',
                            'value': 'anyone',
                            'role': 'reader'})

#SHARABLE LINK
link=file2['alternateLink']

#To use the image in Gsheet we need to modify the link as follows
link=file2['alternateLink']
link=link.split('?')[0]
link=link.split('/')[-2]
link='https://docs.google.com/uc?export=download&id='+link
print link

这篇关于是否可以从C#中的GDrive API获取带有共享选项的下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆