0

I'm following the tutorial here

So far so good but the upload example give me errors. The code:

from dropbox import client, rest, session

f = open('txt2.txt') # upload a file
response = client.put_file('/magnum-opus.txt', f)
print "uploaded:", response

The error:

Traceback (most recent call last):
  File "dropbox_ul.py", line 4, in <module>
    response = client.put_file('/magnum-opus.txt', f)
AttributeError: 'module' object has no attribute 'put_file'

Where did I go wrong?

EDIT: The new code I'm trying. This is actually from the dropbox developer website. As I stated earlier, I did go through the authentication and setup:

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website
APP_KEY = 'iqxjea6s7ctxv9j'
APP_SECRET = 'npac0nca3p3ct9f'

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'dropbox'

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# 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()

f = open('txt2.txt')
response = client.put_file('/magnum-opus.txt', f)
print "uploaded:", response

folder_metadata = client.metadata('/')
print "metadata:", folder_metadata

f, metadata = client.get_file_and_metadata('/magnum-opus.txt',rev='362e2029684fe')
out = open('magnum-opus.txt', 'w')
out.write(f)
print(metadata)

and the error:

   url: https://www.dropbox.com/1/oauth/authorize?oauth_token=jqbasca63c0a84m
    Please authorize in the browser. After you're done, press enter.

    linked account: {'referral_link': 'https://www.dropbox.com/referrals/NTMxMzM4NjY5', 'display_name': 'Greg Lorincz', 'uid': 3133866, 'country': 'GB', 'quota_info': {'shared': 78211, 'quota': 28185722880, 'normal': 468671581}, 'email': '[email protected]'}
    Traceback (most recent call last):
      File "dropb.py", line 28, in <module>
        response = client.put_file('/magnum-opus.txt', f)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/client.py", line 149, in put_file
        return RESTClient.PUT(url, file_obj, headers)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 146, in PUT
        return cls.request("PUT", url, body=body, headers=headers, raw_response=raw_response)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 113, in request
        raise ErrorResponse(r)
    dropbox.rest.ErrorResponse: [403] 'The provided token does not allow this operation'
0

2 Answers 2

2

You haven't initialized the client object. Refer to the tutorial again and you'll see this:

client = client.DropboxClient(sess)

The sess object must also be initialized before calling the client module's DropboxClient method:

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

You should have all the required parameters (i.e., APP_KEY, APP_SECRET, ACCESS_TYPE) assigned to you when you register your application.

Sign up to request clarification or add additional context in comments.

12 Comments

Tried to add the lines with all the authentication keys and it gave me an authentication error.
On the tutorial page, look under the section marked "In Conclusion". You'll see where they recommend setting APP_KEY, APP_SECRET, and ACCESS_TYPE. These values are usually hexadecimal string values that might look like (but not exactly) this "fd23433abc33fg3g33g90". These are provided to you when you sign up as a developer. If you don't set these properly, then you'll get an authentication error.
You can sign in to see the app keys for your project here: dropbox.com/developers/start/setup#python
Ah, make sense. I did sign up and an other example works just fine. I wonder, is there a way to avoid having to allow access (copy link, open it in browser, click 'Allow', hit enter in terminal...)?
As I said, the signing up is done. I have the API keys, the terminal prompts me to open the URL in the browser, then click on Allow... all done. Yet the example is not working.
|
0

I followed the edited code of yours and things worked out perfectly.

from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website
app_key = 'enter-your-app_key'
app_secret = 'enter-your-app_secret'

ACCESS_TYPE = 'dropbox'

sess = session.DropboxSession(app_key, app_secret, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# 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()

f = open('/home/anurag/Documents/sayan.odt')
response = client.put_file('/sayan.odt', f)
print "uploaded:", response

Notice the response and file location on your system, in your code that doesn't matches.

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.