是否可以从 C# 中的 GDrive API 获取带有共享选项的下载链接?

Fra*_*r23 0 c# google-drive-api

我想将文件上传到 GDrive 并为所有拥有该链接的用户获取下载链接。目前我成功上传文件并获取下载链接。但我不知道如何获取所有用户的下载链接。有人可以帮我解决这个问题吗?

ram*_*ram 5

您必须编辑文件的权限以使其可供所有用户访问。

  • 我已经做了python版本。C# 将有相同的实现。
  • https://developers.google.com/sheets/api/quickstart/python
  • 使用上面的链接创建项目并下载client_secret.json
  • 在client_secret.json所在的同一位置创建一个 python 文件 。
  • 然后使用下面的代码上传并创建可共享链接。

代码:

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
Run Code Online (Sandbox Code Playgroud)