'How can I get share link of file/folder by path to the file on insalled google drive in python?
I installed Google Drive on my computer (Windows 11 x64) to drive G:\
I want to be able to get a shared link for a specific file/folder that I have a path to.
Google Drive will have duplicate file/folder.
How can i do this whith python?
Thanks in advance
Edited:
I managed to get a link for specific file name, but now I have a problem if there are 2+ files with the same file name in Google Drive.
For example I want link of this file G:\RootFolder\Subfolder1\Subfolder2\myfile.txt but there is another file with same name G:\RootFolder\Subfolder3\Subfolder4\Subfolder5\myfile.txt. How can I give link only for G:\RootFolder\Subfolder1\Subfolder2\myfile.txt ?
Solution 1:[1]
from Google import Create_Service
CLIENT_SECRET_FILE = 'client-secret.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
# Update Sharing Setting
file_id = '<file id>'
request_body = {
'role': 'reader',
'type': 'anyone'
}
response_permission = service.permissions().create(
fileId=file_id,
body=request_body
).execute()
print(response_permission)
# Print Sharing URL
response_share_link = service.files().get(
fileId=file_id,
fields='webViewLink'
).execute()
print(response_share_link)
# Remove Sharing Permission
service.permissions().delete(
fileId=file_id,
permissionId='anyoneWithLink'
).execute()
Solution 2:[2]
%DATE%, %TIME%, %USERDOMAIN%, and %USERNAME% are all automatic variables you can use.
Something similar to echo|set/p="%DATE% %TIME% - %USERDOMAIN% - %USERNAME%" && echo; >> log.txt would accomplish what you want.
I like using echo|set/p="" because quoted strings are less likely to cause problems, like if %USERNAME% or %USERDOMAIN% contains a character that causes cmd to think the echo command is over before it's supposed to be. && echo; creates a linebreak.
Solution 3:[3]
Just add the requirement into the first line and ensure you exit before the logging
@echo off &Title "%~0" &echo Run %date%%time% by %userprofile%>>"%~0"
rem Your main task goes here
exit /b
Logging starts here
Run 2021-12-23 1:12:02.25 by C:\Users\K
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | K J |
