Skip to content

Instantly share code, notes, and snippets.

@nk9
Last active April 19, 2023 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nk9/2b41ec0f241bc1cc640192f2c367bf3d to your computer and use it in GitHub Desktop.
Save nk9/2b41ec0f241bc1cc640192f2c367bf3d to your computer and use it in GitHub Desktop.
Python script to get the URL of a file in the Dropbox folder
#
# Don't use this version, it is out of date.
# See the newest version in the GitHub repository:
#
# https://github.com/nk9/get_dropbox_link
#
#!/usr/local/bin/python3
# coding=utf-8
# Copyright 2021 Nick Kocharhook
# MIT Licensed
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# import dropbox
# from dropbox.exceptions import ApiError
# import argparse
# from pathlib import Path as P
# import sys
# import json
# import logging
# # Add OAuth2 access token here.
# # You can generate one for yourself in the App Console.
# # See <https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/>
# TOKEN = ''
# # Either 'personal' or 'business'. Must match the account which generated
# # the TOKEN above.
# # See <https://help.dropbox.com/installs-integrations/desktop/locate-dropbox-folder>
# ACCOUNT_TYPE='personal'
# def main():
# local_dbx_path = None
# args = parseArguments()
# if args.verbose:
# logging.basicConfig(level=logging.DEBUG)
# with dropbox.Dropbox(TOKEN) as dbx:
# try:
# dbx.users_get_current_account()
# except AuthError:
# sys.exit("ERROR: Invalid access token; try re-generating an "
# "access token from the app console on the web.")
# try:
# with open(P.home() / '.dropbox/info.json') as jsonf:
# info = json.load(jsonf)
# local_dbx_path = info[ACCOUNT_TYPE]['path']
# except Exception:
# logging.error("Couldn't find Dropbox folder path")
# sys.exit(1)
# for path in args.paths:
# try:
# p = P(path).absolute()
# logging.debug(f'Processing file at path {p}')
# relp = p.relative_to(local_dbx_path)
# dbx_path = f"/{relp}"
# except Exception as e:
# logging.error(str(e))
# sys.exit(1)
# try:
# link = dbx.sharing_create_shared_link(dbx_path)
# print(link.url)
# except ApiError as e:
# logging.error(str(e))
# sys.exit(1)
# def parseArguments():
# parser = argparse.ArgumentParser(description='Fetch Dropbox URL for path')
# parser.add_argument('paths', type=str, nargs='+', help='paths to files')
# parser.add_argument('--verbose', '-v', action='store_true', help='toggle verbose mode')
# args = parser.parse_args()
# return args
# if __name__ == '__main__':
# main()
@nk9
Copy link
Author

nk9 commented Jul 3, 2021

  1. Install the dropbox Python SDK:
pip3 install dropbox
  1. Create a Dropbox app on the Dropbox App Console.
  2. Give the app an access type of Full Access.
  3. Create it.
  4. Change the Permissions settings to have a scope of sharing.write.
  5. Reload the page. (This is in case you've already created an access token. Once you change the permissions, you need to generate a new token!)
  6. Generate a new access token.
  7. Use this to update the TOKEN variable on line 37.
  8. If you use a Dropbox Business account, change the ACCOUNT_TYPE variable on line 42.
  9. Place this script in ~/bin or wherever is appropriate (presumably in your PATH).
  10. Set permissions:
chmod +x ~/bin/get_dropbox_url.py

Now you can call it to generate links.

$ get_dropbox_url.py ~/Dropbox/file.txt
https://www.dropbox.com/s/wlz10rosbw29r1t/file.txt?dl=0
$

@nk9
Copy link
Author

nk9 commented Jan 17, 2023

I've moved this to a repository and expanded on the instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment