Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DropBox tokens expire after 4 hours #362

Open
YouveGotMeowxy opened this issue Jan 19, 2024 · 23 comments
Open

DropBox tokens expire after 4 hours #362

YouveGotMeowxy opened this issue Jan 19, 2024 · 23 comments

Comments

@YouveGotMeowxy
Copy link

@sebastianfeldmann

Any chance of getting this fixed pretty soon? The key expires every 4 hours now, meaning adding to a daily cron job always fails, because the key has expired. Then in order to get it working again the user has to go back to their dbox app page, regen an 4hr key, and replace them all in their .json config. For me right now that's 9 locations in the configs. :)

btw, just wanted to add, this is one of the greatest apps ever, lol. Once it's all setup and running smooth, it's soo awesome.

Originally posted by @YouveGotMeowxy in #356 (comment)

@YouveGotMeowxy YouveGotMeowxy changed the title > Thanks for the heads up. DropBox tokens expire after 4 hours Jan 19, 2024
@sebastianfeldmann
Copy link
Owner

thanks @YouveGotMeowxy :)

To be honest, I'm not sure how fast I can fix this.
I would need to get into this refresh token thing and implement the logic to reissue valid tokens and so on.

I hope I can start looking into it maybe end of next week but I really can't make any promises :/

Plan so far:

  1. Make refresh token configurable
  2. Make sure to retrieve valid token before the sync
  3. Execute the actual sync

@YouveGotMeowxy
Copy link
Author

@sebastianfeldmann ok, thank you for any work on it. :)

I don't really know if this will help you out any, but I was just looking around about how to get a permanent token and came across this; maybe it might help ya out as well.

https://www.codemzy.com/blog/dropbox-long-lived-access-refresh-token

@YouveGotMeowxy
Copy link
Author

4 months since this issue; any forward-movement happening? :)

@jmcdougal
Copy link

jmcdougal commented Jul 11, 2024

I came across this post as I was having the same issue as I use Dropbox as well for my backups. I resolved it by getting the application access code as mentioned here: https://www.codemzy.com/blog/dropbox-long-lived-access-refresh-token. Using CURL, I retrieved the long lived refresh token using the application access code with a grant type of "authorization_code". I then created another CURL request to Dropbox asking for the short lived access token that I store in a MySQL database. To do that, you use the long lived refresh token from the first CURL request with the grant type of "refresh_token".

An additional PHP script programmatically rebuilds the phpbu.xml file inserting the current refresh token into the line of <option name="token" value="<YOUR_CURRENT_SHORT_LIVED_ACCESS_TOKEN"/> . Using file_put_contents at the end of this script, the previous phpbu.xml file is replaced with a copy that has the newly updated token.

The backup runs twice daily using multiple cron jobs. The first gets the updated short lived access token. The second updates the phpbu.xml script and the third runs the backup. Each cron job is separated by five minutes. This could be simplified to one cron job and script, but works fine as is and I have moved on to other projects.

Anyway, thought this might be helpful as you navigate this issue. Overall, PHPBU is an excellent tool. Great job Sebastian!

@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Nov 15, 2024

With version 6.0.26 the Dropbox integration was fixed.

You need to create an App in your Dropbox AppConsole.
Generate an Access Token and provide token, appKey and appSecret in the phpbu configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<sync type="dropbox">
  <!-- mandatory -->
  <option name="token" value="mycrazylongapitokenthatigotfromdropbox" />

  <!-- mandatory -->
  <option name="appKey" value="myAppKey" />

  <!-- mandatory -->
  <option name="appSecret" value="myAppSecret" />

  <!-- mandatory -->
  <option name="path" value="/some/dir" />
</sync>

@YouveGotMeowxy
Copy link
Author

yaayy! thank you!

I just tried to add those 2 new key:values and try with 6.0.26, and I get this error though?

Parse error: syntax error, unexpected 'o020000' (T_STRING), expecting ';' in phar:///home/user/public_html/dbbackup/phpbu/phpbu-6.0.26.phar/lib/sebastian-environment/Console.php on line 115

The previous version 5.2.10 was working with the exception of the expiring token (IIRC) and the only things I've added now are the appkey and appsecret. It now looks like this:

      "syncs": [
        {
          "type": "dropbox",
          "options": {
            "token": "sl.superlongtoken",
            "app key": "shortertoken",
            "app secret": "anothershortoken",
            "path": "/Backups/DB/A2/MyServer"
          }
        }
      ]

My PHP versions is 7.4.33

Any idea?

@sebastianfeldmann
Copy link
Owner

Yes, version 6 requires php 8

I think the dependencies are PHP 8 only but I can check and try to back port the feature to the 5.0 release version.
But I'm not sure that's possible.

@YouveGotMeowxy
Copy link
Author

ok! I was able to change to PHP 8.3 and got it to run. I'm getting a sync error now, but gonna look into it!

@YouveGotMeowxy
Copy link
Author

My fault, I got it and it's working great! You just made my whole day, lol. thank you so much! <3

@YouveGotMeowxy
Copy link
Author

:(

I guess I spoke too soon. It worked after I had generated the new superlongtoken, but then overnight when my cronjob ran the same command-line I woke up to the same error that token expired.

            "class": "phpbu\\App\\Backup\\Sync\\Exception",
            "message": "{\"error_summary\": \"expired_access_token\/...\", \"error\": {\".tag\": \"expired_access_token\"}}",
            "file": "phar:\/\/\/home\/user\/public_html\/dbbackup\/phpbu\/phpbu-6.0.26.phar\/Backup\/Sync\/Dropbox.php",
            "line": 138

I don't know which specific info you may need from me to help out, just let me know.

@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Nov 20, 2024

I think I have found a way with refresh tokens.
I will test it again tomorrow, if it works I will release a new version and a guide how to obtain a refresh token for your app.

sebastianfeldmann added a commit that referenced this issue Nov 24, 2024
In order to support short-lived tokens we have to provide a
refresh token, which can be obtained from the Dropbox api.

Fixes issue #362
@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Nov 24, 2024

It is finally fixed!
At least it works on my machine ;)

The thing you have to be aware of is that you have to provide a RefreshToken not an AccessToken.
in order to generate one you have to make a Dropbox API call.

You can use the dropbox-helper package I created to make the call and receive your refresh key.

With that key, that you should never share with anybody, you can create short-lived api AccessTokens.

@YouveGotMeowxy
Copy link
Author

Thank you for keeping on top of this @sebastianfeldmann :)

I'm probably being a doofus but when I run the new script, I just get this:

bin/dropbox
Please enter the AppKey: q6**********
Please enter the AppSecret: ex*********

https://dropbox.com/oauth2/authorize?client_id=q6**********&response_type=code&token_access_type=offline' - Log in with Dropbox

Please enter the code: 

I don't get any return code to enter?

I tried logging into both that particular app's settings page as well as the main account page, but see no "Refresh Token" or "Code" to enter into the new script?

also, a couple other questions:

once I do get my refresh token, do I just delete the original line (I'm using json btw):

"token": "sl.CA-jsuperlongstring",

and replace it with (in json version):

<option name="refreshToken" value="MY_REFRESH_TOKEN" />

and from then on does that same refresh token always work, or do I need to repeat the helper script every so often and manually edit the token in my configs?

Sorry if I'm asking imbecilic questions: :p

@sebastianfeldmann
Copy link
Owner

Not at all, you have to go to the URL and retrieve the AppCode and put that in the prompt on the cli to finally get the RefreshToken.

@sebastianfeldmann
Copy link
Owner

I added the URL step to the README of the helper package.

@YouveGotMeowxy
Copy link
Author

ok, thank you, got it! should I just entirely replace my original "token:" in the configs? or keep it there and just add the new refreshtoken to it?

@sebastianfeldmann
Copy link
Owner

Remove the token from the config and then add refreshToken to your config.

The token is not required anymore :)

@YouveGotMeowxy
Copy link
Author

Also, just a teeny tweak to the message in the script might help out stupid ppl like me, LOL:

from: Log in with Dropbox
to Log into this URL with Dropbox

I had assumed the link shown was some sort of returned URL by DropBox that was showing a sort of error in a way, and to go to my browser to login to DropBox. ope! lol

@sebastianfeldmann
Copy link
Owner

Good idea, I will update the message 👍

@YouveGotMeowxy
Copy link
Author

So far so good! WEWT!!! TYSM

I don't have many heroes, but you've just been added to my list! :p

I have them set to run nightly on my cron, if it poops out I'll letcha know! :)

Also, none of the following is very important, but would make for very nice feedback enhancements. It seems the backup script waits for all backups to complete before giving any user feedback whatsoever. In the case of backing up many things, including encrypting, sending to DB, etc., it can take a pretty long time, and visually the script/terminal just seems to "hang" with a blinking cursor. It could easily be confused with a freeze.

Maybe initially adding something like "performing backup", "encrypting", "sending to DB", etc. as it's performing each step, and then showing the little chart after each backup is complete, rather than a long pause with a big "dump" at the end? Feedback is always nice. :)

Just an idea! :)

@YouveGotMeowxy
Copy link
Author

Last question tonight so I stop annoying you! lol

Has this been fixed yet? #364

@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Nov 24, 2024

Nope not yet, at least not that I know of, it's up next :)

@YouveGotMeowxy
Copy link
Author

poke @sebastianfeldmann :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants