Forum Discussion

George K.23's avatar
George K.23
New member | Level 2
10 years ago

API session expiry?

Hi everyone,

I'm in the process of creating a Python app which will run on my Raspberry Pi to allow me to control a webcam via Dropbox. I have created an app and generated an access token. In my program, I have the following general structure:

if __name__ == '__main__':

    dbx = dropbox.Dropbox('MY_ACCESS_TOKEN')

    while True:

        if poll_parameter('imagerequest') == 1:

            print 'Image requested'

            ## Image capture code here

            reset_parameter('imagerequest')

        if poll_parameter('exitprogram') == 1:

            print 'Exit requested'

            reset_parameter('exitprogram')

            break

        time.sleep(poll_parameter('delay'))

So, you can see that the program creates a dropbox.Dropbox() session when it first opens, and then enters a loop where it polls certain parameters which are stored as files in the app's folder on Dropbox. Note that the poll_parameter() and reset_parameter() functions use the files_search() and files_move() API functions to search and rename the parameter files as necessary.

So, I started the program running today when I left for work, and I was able to command it for a few hours and it was taking/uploading images fine, but then it stopped responding. I think this is because the dropbox.Dropbox session may have expired. Does this happen after a certain time interval? How can I ensure that my app can continue to stay connected to Dropbox for as long as I want to use it (possibly weeks at a time)?

Perhaps if I put my dbx = dropbox.Dropbox('MY_ACCESS_TOKEN') line at the beginning of every loop, instead of just once at the very beginning of the program?

I also went into my app settings on Dropbox, and my Generated Access Token has disappeared (it just shows the 'Generate' button again). Is this because I left my app running? Why would this delete my access token?

Thanks a lot for the help, I'd love to get this little project off the ground, to the point where it will work for weeks on end so I can remotely monitor my webcams on holiday!

5 Replies

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    These sessions don't expire, nor do the access tokens themselves. (Note that access tokens can be explicitly revoked though, e.g.,via https://www.dropbox.com/account/security .) Further, the App Console lets you generate an access token for your account and app, but it will only show you that token when you generate it. If you return to or reload that page, the previously generated token won't still be showing. Generating another won't revoke the first one though.

    Anyway it sounds like at a certain point, some Dropbox API calls stopped returning the result you expected, or they started failing. So, to investigate this issue, can you share the error/output you got from the app when it started failing? The corresponding code may also be helpful.

  • George K.23's avatar
    George K.23
    New member | Level 2
    10 years ago

    Hi, thanks for the reply!

    I found that my program terminated with the following error:

    ---

    Traceback (most recent call last):
    File "DropCamScript.py", line 29, in <module>
    if poll_cam_parameter(main_dbx, 'imagerequest') == 1:
    File "DropCamScript.py", line 9, in poll_cam_parameter
    sch = dbx.files_search('', param + '=', 0, 1)
    File "/usr/local/lib/python2.7/dist-packages/dropbox/base.py", line 436, in files_search
    None)
    File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 175, in request
    request_binary)
    File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 256, in request_json_string_with_retry
    request_binary)
    File "/usr/local/lib/python2.7/dist-packages/dropbox/dropbox.py", line 316, in request_json_string
    verify=True,
    File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 511, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
    File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 426, in send
    raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

    ---

    The code is basically as I posted it in the first post (with the addition of some Pygame code to capture from the camera). The only Dropbox functions I use are:

    - dropbox.Dropbox()

    [once, at the beginning of the program, using my previously generated access token]

    - files_search()

    [In my main loop (structure visible in first post), I search the app's dropbox folder for files with different names, and then extract the numbers from these filenames to tell the program what to do. This function appears to have produced the above error. Also, I manually edit the filenames via dropbox to instruct the program to take a picture, wait a certain time between loop iterations, or to exit the loop. Perhaps it's my manual intervention interfering with the files_search() function that caused something strange to happen?]

    - files_move() [I use this to rename files from their current filename to a new filename which contains a specified numeric value. For example, if the program finds a file called 'imagerequest=1', it will use the files_move() function to rename it to 'imagerequest=0']

    ...and that's pretty much it! I don't have any exception handling in the program yet, which is why it stopped responding to my dropbox commands when it ran into this issue. Does the error stack provide any guidance?

    Thanks a lot for the help!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    Thanks! The ConnectionError error can be caused by a number of different things, but it is just indicating there was an issue with the HTTPS connection for the API call, e.g., to due to network issues on your side, or transient server issues on our side. In any case, the right thing to do is just implement some automatic retrying to handle connection issues like this. 

  • George K.23's avatar
    George K.23
    New member | Level 2
    10 years ago

    OK, cool - yeah I guess you can't always guarantee the internet will cooperate! I've added simple exception handling to all my dropbox calls so that at least the program won't terminate. Do you think I have to re-create the dropbox.Dropbox('ACCESS_TOKEN') object in the event of a network exception, or will I be OK to just re-try the search/move functions next time round in the loop?

    Thanks!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    No, just retrying (maybe with an exponential backoff) without recreating that object should be fine.

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!