Skip to content

Instantly share code, notes, and snippets.

@ayushgoel
Created October 21, 2011 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayushgoel/1304956 to your computer and use it in GitHub Desktop.
Save ayushgoel/1304956 to your computer and use it in GitHub Desktop.
Testing Dropbox new Python API
## author: Ayush Goel
## Python 2.7 used
## get the Dropbox new API from https://www.dropbox.com/developers/
## mail: ayushgoel111@gmail.com
## put in your APP KEY and APP SECRET KEY
## do remember to install oauth, setuptools, simplejson
## not included by default in Python 2.7 installation
## http://pypi.python.org/pypi/setuptools
## http://pypi.python.org/pypi/oauth/1.0.1
## http://pypi.python.org/pypi/simplejson/
# Include the Dropbox SDK libraries
from dropbox import client, rest, session
# Get your app key and secret from the Dropbox developer website
APP_KEY = 'INSERT_APP_KEY_HERE'
APP_SECRET = 'INSERT_SECRET_HERE'
# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = "app_folder" #'INSERT_ACCESS_TYPE_HERE'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()
while True:
url = sess.build_authorize_url(request_token)
print "url:", url
import webbrowser
webbrowser.open_new_tab(url)
print "You have been redirected to the authorization page."
print "Please do the authorization within 5 minutes else the URL would expire."
print "Press ENTER here once you are done. To create the url again, enter any character"
#print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
s=raw_input()
if s=='':
break
# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
client = client.DropboxClient(sess)
print "linked account:", client.account_info()
## The client object is what is required for the whole
## app buildup by anyone
## anyways, lets have a look at some things of interest of
## all those tokens we just saw
## the url we produced above
print url
#'https://www.dropbox.com/1/oauth/authorize?oauth_token=n8yyjthf92hv1g5'
print sess.API_CONTENT_HOST
# 'api-content.dropbox.com'
print sess.API_HOST
# 'api.dropbox.com'
print sess.API_VERSION
# 1
print sess.WEB_HOST
# 'www.dropbox.com'
print sess.is_linked()
# True
print sess.locale
# nothing None
print sess.root
# 'sandbox'
print sess.signature_method.get_name()
# 'PLAINTEXT'
## Very important to NOTICE: every token we generated
## has two unique identifiers (key, secret)
print request_token.key
# 'n8asydyfjrtth4f92hv1g5'
print request_token.secret
# 'quwe1r345ouzae1mwijwum'
print request_token.verifier
# None
print access_token.key
# 'd2ert57rjytjzd8wc39iof'
print access_token.secret
# 'wfjwlfert5ho0de2qy44'
print access_token.callback_confirmed
# None
print access_token.get_callback_url()
# None
## file_create_folder() creates a folder in the app folder given to you.
## The folder name is passed as an argument
## a dict is returned giving details of the folder
## errors are raised otherwie (see the documentation)
print client.file_create_folder("folder1")
## {
## u'size': u'0 bytes',
## u'rev': u'104721f34',
## u'thumb_exists': False,
## u'bytes': 0,
## u'modified': u'Fri, 21 Oct 2011 19:48:25 +0000',
## u'path': u'/folder1',
## u'is_dir': True, # memory management
## u'icon': u'folder',
## u'root': u'app_folder', # authotization
## u'revision': 1
## }
## client.put_file("C:\Users\Ayush\Desktop\dropify.txt","folder1/")
##
##Traceback (most recent call last):
## File "<pyshell#31>", line 1, in <module>
## client.put_file("C:\Users\Ayush\Desktop\dropify.txt","folder1/")
## File "C:\Users\Ayush\Applications\python_pkg\dropbox-python-sdk-1.2\dropbox-1.2\dropbox\client.py", line 147, in put_file
## return RESTClient.PUT(url, file_obj, headers)
## File "C:\Users\Ayush\Applications\python_pkg\dropbox-python-sdk-1.2\dropbox-1.2\dropbox\rest.py", line 142, in PUT
## return cls.request("PUT", url, body=body, headers=headers, raw_response=raw_response)
## File "C:\Users\Ayush\Applications\python_pkg\dropbox-python-sdk-1.2\dropbox-1.2\dropbox\rest.py", line 109, in request
## raise ErrorResponse(r)
##ErrorResponse: [400] {u'path': u"Path 'C:\\Users\\Ayush\\Desktop\\dropify.txt' can't contain \\"}
## correct way of uploading a file
f=open("C:\Users\Ayush\Desktop\dropify.txt")
print client.put_file("folder1/dropify.txt",f)
## {
## u'size': u'34 bytes',
## u'rev': u'204721f34',
## u'humb_exists': False,
## u'bytes': 34,
## u'modified': u'Fri, 21 Oct 2011 19:57:39 +0000',
## u'path': u'/folder1 (1)',
## u'is_dir': False,
## u'icon': u'page_white',
## u'root': u'app_folder',
## u'mime_type': u'application/octet-stream',
## u'revision': 2
## }
##read a file from dropbox
## we are actually reading the same file we just
## uploaded. It's a text document
a=client.get_file("folder1/dropify.txt")
print a.fileno()
# 588
## get headers of the file we are reading
## a list of tuples is returned
## difference between tuples and lists will be covered later
print a.getheaders()
## [
## ('content-length', '34'),
## ('accept-ranges', 'bytes'),
## ('server', 'dbws'),
## ('connection', 'keep-alive'),
## ('etag', '2n'),
## ('pragma', 'public'),
## ('cache-control', 'max-age=0'),
## ('date', 'Fri, 21 Oct 2011 19:59:28 GMT'),
## ('content-type', 'text/plain; charset=ascii')
## ]
print a.read()
## the contents
## please don't do this on your "big files"
## may slow down or clog your app as memory requirements would go very high
## some additional data about the file
print a.reason
# 'OK'
print a.status
# 200
print a.strict
# 0
print a.version
# 11
print a.chunk_left
# 'UNKNOWN'
print a.chunked
# 0
print a.begin()
# None
print a.read()
# ''
## another list of headers
s=a.getheaders()
# I am actually printing the headers beautifully :)
for i in s:
print "%15s%s%20s"%(i[0]," : ", i[1])
## content-length : 34
## accept-ranges : bytes
## server : dbws
## connection : keep-alive
## etag : 2n
## pragma : public
## cache-control : max-age=0
## date : Fri, 21 Oct 2011 19:59:28 GMT
## content-type : text/plain; charset=ascii
print client.metadata('/')
##{
## u'hash': u'00d3e63a8e91467dddaf18d04b206e57',
## u'thumb_exists': False,
## u'bytes': 0,
## u'path': u'/',
## u'is_dir': True,
## u'icon': u'folder',
## u'root': u'app_folder', u
## 'contents': [
## {
## u'size': u'0 bytes',
## u'rev': u'104721f34',
## u'thumb_exists': False,
## u'bytes': 0,
## u'modified': u'Fri, 21 Oct 2011 19:48:25 +0000',
## u'path': u'/folder1',
## u'is_dir': True,
## u'icon': u'folder',
## u'root': u'dropbox',
## u'revision': 1
## },
## {
## u'size': u'34 bytes',
## u'rev': u'204721f34',
## u'thumb_exists': False,
## u'bytes': 34,
## u'modified': u'Fri, 21 Oct 2011 19:57:39 +0000',
## u'path': u'/folder1',
## u'is_dir': False,
## u'icon': u'page_white',
## u'root': u'dropbox',
## u'mime_type': u'application/octet-stream',
## u'revision': 2
## }
## ],
## u'size': u'0 bytes'
##}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment