Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Nicholas H.2's avatar
Nicholas H.2
New member | Level 1
10 years ago

Generated Access Token not working for authentication (Python)

Hello,

I generated an access token on my app page, but it does not seem to be working. I am using it in place of the token that is normally generated when give it access from the webpage. Here is my current code:

import dropbox
app_key = 'xxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxx'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()
code = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token, user_id = flow.finish(code)

I am definitely putting in the right access token, secret, and key.

4 Replies

Replies have been turned off for this discussion
  • Nicholas H.2's avatar
    Nicholas H.2
    New member | Level 1
    10 years ago

    I should add, it does give me this error:

    Traceback (most recent call last):
      File "pictures.py", line 15, in <module>
        access_token, user_id = flow.finish(code)
      File "C:\Python27\lib\site-packages\dropbox-2.2.0-py2.7.egg\dropbox\client.py"
    , line 1398, in finish
        return self._finish(code, None)
      File "C:\Python27\lib\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 "C:\Python27\lib\site-packages\dropbox-2.2.0-py2.7.egg\dropbox\rest.py",
    line 316, in POST
        return cls.IMPL.POST(*n, **kw)
      File "C:\Python27\lib\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 "C:\Python27\lib\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'
    
  • Try just this instead:

    from dropbox.client import DropboxClient
    access_token = 'xxxxxxx' # <-- your generated access token
    client = DropboxClient(access_token)
    

    It looks like what you're trying to do is use your access token as the "authorization code" for the OAuth flow, but that's not right. The result of the OAuth flow is an access token, and you already have one of those, so you're done!

  • Jay M.1's avatar
    Jay M.1
    New member | Level 1
    10 years ago

    Hi Steve M ,

    I'm using https://www.dropbox.com/developers/core/docs#oa2-authorize to authenticate user and 

    then I'm getting code in return at url.

    Redirect url is like this --> [REDIRECT_URI]?code=ABCDEFG&state=[STATE]

    After that I'm using below codes to get access token from the above
    code.
    flow.start()
    access_token, user_id = flow.finish(code)
    It gives me invalid_grant error.? Can you help please.
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    Hi Jay, if you're using using the redirect flow, i.e., with DropboxOAuth2FlowNoRedirect, you need to pass all of the args to .finish(), so that the state can be checked as well, not just code.

    Also, you only need to call .start() when sending the user off to the auth flow initially, and not again when they come back via the redirect.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!