cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python error when setting up access for my App to Dropbox account

Python error when setting up access for my App to Dropbox account

Mobistudios A.
New member | Level 1

Hi,

I'm new to this and need some help with setting up access with my Dropbox app to a Dropbox account using Python. When running the code I get the error shown below. I have inserted the app and secret codes into it. Plus the app has an API token. Any guidance would be greatly appreciated.

See python code below error message.

Error message

Traceback (most recent call last):

  File "dropbox_test.py", line 2, in <module>

    import dropbox

  File "/Users/ninekay/Desktop/Python/dropbox.py", line 24

    client = dropbox.client.DropboxClient(access_token)

 

Python code being used

 

Include the Dropbox SDK

import dropbox

Get your app key and secret from the Dropbox developer website

app_key = 'qdrn1l679o14y86'

app_secret = '<redacted>'

flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)

Have the user sign in and authorize this token

authorize_url = flow.start()

print '1. Go to: ' + authorize_url

print '2. Click "Allow" (you might have to log in first)'

print '3. Copy the authorization code.'

code = raw_input("Enter the authorization code here: ").strip()

This will fail if the user enters an invalid authorization code

access_token, user_id = flow.finish(code)

client = dropbox.client.DropboxClient(access_token)

print 'linked account: ', client.account_info()

f = open('working-draft.txt', 'rb')

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')

out = open('magnum-opus.txt', 'wb')

out.write(f.read())

out.close()

print metadata

6 Replies 6

Steve M.
Dropbox Staff

Change the name of your Python file. When you do import dropbox, Python is first noticing the file dropbox.py in the current directory and trying to import that. What you want it to actually do is import the Dropbox SDK, so rename your Python file to something other than dropbox.py, and things should work.

Mobistudios A.
New member | Level 1

 Thanks for the answer. we have made progress. Now when making any choice at the menu I get the following error.

  1. Go to: https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=qdrn1l679o14y86
  2. Click "Allow" (you might have to log in first)
  3. Copy the authorization code.
    Enter the authorization code here: 1
    /Library/Python/2.7/site-packages/urllib3-1.11-py2.7.egg/urllib3/util/ssl_.py:97: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
    Traceback (most recent call last):
      File "upload.py", line 18, in <module>
        access_token, user_id = flow.finish(code)
      File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/client.py", line 1398, in finish
        return self._finish(code, None)
      File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/client.py", line 1265, in _finish
        response = self.rest_client.POST(url, params=params)
      File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 316, in POST
        return cls.IMPL.POST(*n, **kw)
      File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 254, in POST
        post_params=params, headers=headers, raw_response=raw_response)
      File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 227, in request
        raise ErrorResponse(r, r.read())
    dropbox.rest.ErrorResponse: [400] u'invalid_grant'

Steve M.
Dropbox Staff

Are you sure you're entering the access code from dropbox.com properly? Also make sure you're only using the code you get once.

Mobistudios A.
New member | Level 1

"Are you sure you're entering the access code from dropbox.com properly"

Yes, I'm using a new access token.

1.

I go to my App console on Dropbox Developers

2.

I open up my app

3.

Then go to Generated access token.

4.

I use that token when it prompts me.

5.

I'm using the terminal on a Mac 10.11.4.

Just upgraded python from 2.7.6 to 2.7.9 and still incurring the same error message.

Error Message

W8006005A54:python ninekay$ python upload.py

  1. Go to: https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=qdrn1l679o14y86

  2. Click "Allow" (you might have to log in first)

  3. Copy the authorization code.

Enter the authorization code here: <REDACTED>

Traceback (most recent call last):

  File "upload.py", line 21, in <module>

    access_token, user_id = flow.finish(code)

  File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/client.py", line 1398, in finish

    return self._finish(code, None)

  File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/client.py", line 1265, in _finish

    response = self.rest_client.POST(url, params=params)

  File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 316, in POST

    return cls.IMPL.POST(*n, **kw)

  File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 254, in POST

    post_params=params, headers=headers, raw_response=raw_response)

  File "/Library/Python/2.7/site-packages/dropbox-2.2.0-py2.7.egg/dropbox/rest.py", line 227, in request

    raise ErrorResponse(r, r.read())

dropbox.rest.ErrorResponse: [400] u'invalid_grant'

Steve M.
Dropbox Staff

You're not supposed to use your generated access token there. As the app says, you should go to the URL it gives you and authorize the app. You'll then receive an access code (not access token) that you need to copy/paste back into the app.

BTW, I've redacted your access token, but you should probably unlink your app (from dropbox.com/account/security) now that your access token has been published here. With an access token, someone can read and write files in your account, so you never want to share an access token with anyone.

Mobistudios A.
New member | Level 1

Steve,

Now have access. thanks a million for your help.

When running in the terminal it never displayed that message about going to the other URL.

So I ran it in my text editor and it showed up. Its the first time I used it.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Mobistudios A. New member | Level 1
  • User avatar
    Steve M. Dropbox Staff
What do Dropbox user levels mean?