cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We are making some updates so the Community might be down for a few hours on Monday the 11th of November. Apologies for the inconvenience and thank you for your patience. You can find out more 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: 

Django integrate with Dropbox

Django integrate with Dropbox

HADES
Helpful | Level 5
Go to solution

Hello everyone I met an issue with the Dropbox access token when I generated it on the Dropbox console app and used it in setting.py like this.


MEDIA_URL = '/media/'

MEDIA_ROOT = BASE_DIR / 'Media'

 

# Store media files in Dropbox
DROPBOX_ACCESS_TOKEN = '<My_token>'

STORAGES = {
"default": {
"BACKEND": "storages.backends.dropbox.DropboxStorage",
"OPTIONS": {
"access_token": DROPBOX_ACCESS_TOKEN,
},
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},

}

DROPBOX_ROOT_PATH = 'Media'

DROPBOX_TIMEOUT = '100'

DROPBOX_WRITE_MODE = 'add'

Then I run the server and it raises me:

Invalid setting 'access_token' for DropboxStorage 

 Has anyone had the same issue as me? 

1 Accepted Solution

Accepted Solutions

Здравко
Legendary | Level 20
Go to solution

Hi @HADES,

Just keep in mind that no any real Dropbox API call can be seen in the code you shared. Another thing is that all access tokens are short lived, so wherever you perform actual API calls it's likely to fail after some time (typically 4 hours). For long term access you need refresh token. If a supported SDK is in use just proper initialization is enough (for the rest SDK takes care), otherwise tracing access token validity and refresh whenever needed should be performed.

Hope this gives direction.

View solution in original post

7 Replies 7

Здравко
Legendary | Level 20
Go to solution

Hi @HADES,

Just keep in mind that no any real Dropbox API call can be seen in the code you shared. Another thing is that all access tokens are short lived, so wherever you perform actual API calls it's likely to fail after some time (typically 4 hours). For long term access you need refresh token. If a supported SDK is in use just proper initialization is enough (for the rest SDK takes care), otherwise tracing access token validity and refresh whenever needed should be performed.

Hope this gives direction.

Greg-DB
Dropbox Staff
Go to solution

I see Здравко already helpfully offered guidance on this, but for reference you can find more information about how this works on Dropbox at the following resources:

HADES
Helpful | Level 5
Go to solution

Hello everyone some day ago I got and used Dropbox SDKs and their refresh token to upload files and images.
I setting like this: 
 # Setting for Dropbox
dbx = dropbox.Dropbox(
oauth2_refresh_token = My_token['refresh_token'],
app_key = My_token['app_key'],
app_secret = My_token['app_secret'],
)

with file_name.open('rb') as f:
dbx.files_upload(f.read(), f'/{file_name}')

 

then I can upload my files and I can see my files locally: Apps/Media_django/{file_name} 


but now I need to store that link to display it in my case as images:
path = '/Apps/Media_django/{fiel_name}'
share_link_metadata = dbx.sharing_create_shared_link_with_settings(path)
url_avatar = share_link_metadata.url

and I get that path not found all the time when I try path= '/Media_django/{...}', or '/{...}' it still can not find my path
I need some help here.

Здравко
Legendary | Level 20
Go to solution

Hi @HADES,

Glad to hear your upload is working already. 😉 Why do you use different path when you try get link? The path has to be the same one used on file upload or received on listing your containing folder. Don't assume something else, like what you see in your web browser or based on local Dropbox folder content! All paths for "App folder" app type are rooted in the app's automatically created own folder, not the entire account.

Hope this helps.

 

PS: In context of:


@HADES wrote:

... it still can not find my path ...


Evaluate the result of:

listRes = dbx.files_list_folder('')

... to see what you have there (in 'listRes'). 😉

HADES
Helpful | Level 5
Go to solution

Thank you, I finally uploaded and got an image link to display for my application. 
My code for someone who wants to store image and display them: 
def upload_file(dbx, file, file_name):
with file.open('rb') as f:
dbx.files_upload(f.read(), f'/{file_name}')


# Get and create url of image
def get_and_share_image(dbx, file_name):
# Query all files to get file_name
all_files = dbx.files_list_folder('').entries
for file in all_files:
if isinstance(file, dropbox.files.FileMetadata) and file.name == file_name:

# Create avatar_url
generate_image_url = dbx.sharing_create_shared_link(file.path_lower)
image_url = generate_image_url.url



Greg-DB
Dropbox Staff
Go to solution

Please note that calling only files_list_folder is not guaranteed to return all of the items in the specified folder. You should use both files_list_folder and files_list_folder_continue to make sure you can retrieve all entries. Refer to the linked documentation for more information.

Здравко
Legendary | Level 20
Go to solution

@HADES, You don't need listing at all. Just proper path formatting would be enough.  I proposed you list your folder just to see what's there - i.e. for debug - nothing more.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    HADES Helpful | Level 5
What do Dropbox user levels mean?