0
votes

I'm new in Python and start playing around to access different API and now I got to Dropbox APIs. But I got a "invalid_grant" access when I try to access my own account.

import dropbox
app_key = 'AAAA'
app_secret = 'BBBB'

flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
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 = 'CCCC'

access_token, user_id = flow.finish(code)

When I runt this script I got following output.

Traceback (most recent call last): File "PATH/dropboxdownload.py", line 14, in access_token, user_id = flow.finish(code) File "PATH\Python35-32\lib\site-packages\dropbox-2.2.0-py3.5.egg\dropbox\client.py", line 1398, in finish return self._finish(code, None) File "PATH\Python35-32\lib\site-packages\dropbox-2.2.0-py3.5.egg\dropbox\client.py", line 1265, in _finish response = self.rest_client.POST(url, params=params) File "PATH\Python35-32\lib\site-packages\dropbox-2.2.0-py3.5.egg\dropbox\rest.py", line 316, in POST return cls.IMPL.POST(*n, **kw) File "PATH\Python35-32\lib\site-packages\dropbox-2.2.0-py3.5.egg\dropbox\rest.py", line 254, in POST post_params=params, headers=headers, raw_response=raw_response) File "PATH\Python35-32\lib\site-packages\dropbox-2.2.0-py3.5.egg\dropbox\rest.py", line 227, in request raise ErrorResponse(r, r.read()) dropbox.rest.ErrorResponse: [400] 'invalid_grant' Process finished with exit code 1

2
I assume AAAA, BBBB, and CCCC aren't real values, but are you in fact hardcoding a value for the code? That truly needs to be a value the user gets from the website. If you already have an access token that you want to use, then just skip all of this code and assign access_token to that value and move on.user94559
Hi,That is correct, AAAA, BBBB and CCCC is fake values when I public the code on the web. But in fact this is just a Test Script, when I finally got the Query done I think I can really do what I want. But to give you some more information, what I try to do is create a file in Dropbox and I want to download it once a day and load it to my Tweppy Python script so I can easy add new hashtags and it will automatic update my Tweppy filter.Christian Issen Petersson
I found the issue with the script but it didn't solve my problem. I can now get hold of my Account Details but I need to assign for a new authorization code each time I run the Python script. I need a way that I can download this file once a day without needing to sign in at Dropbox and get hold of a new authorization code. Thanks ChristianChristian Issen Petersson
Once you have an access token (either from this script or generating a token from the app console), just reuse that. You can't reuse authorization codes, just access tokens.user94559

2 Answers

0
votes

I've solved the problem, and my problem was that the token was expired.

you may try getting detail error message using following codes

try :
    access_token,user_id = flow.finish(code)
except dropbox.rest.ErrorResponse as err:
    print("this"+str(err.body))

If your error is token expired, then you need to go to the link from authorise_url, and then get new token.

0
votes

If this error continues use the following link:

https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=**your ID**

Make sure to add your ID to the client_id parameter.