Forum Discussion

kdss-ja's avatar
kdss-ja
Explorer | Level 3
3 years ago

Trying to get token to work or set expiration date to long time

I've copied the example code in SimpleTest and got access token working but expires after short time

I've just want very simple way to write and read files from my drop box programmatically. (and share the app with others on same drop box ) 

 

GetAccessToken works but it expires after short time expires

Is there way to set expiration date or refresh the token, I've tried for several hours

 

string[] scope = { "account_info.read", "files.content.read", "files.content.write" };
await client.RefreshAccessToken(scope); //didn't work

please send me C# code example not curl

 

thx

6 Replies

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

    Dropbox is no longer offering the option for creating new long-lived access tokens, and does not offer an option for setting an arbitrary access token expiration length. Dropbox is switching to only issuing short-lived access tokens (and optional refresh tokens) instead of long-lived access tokens. You can find more information on this migration here.

    Apps can still get long-term access by requesting "offline" access though, in which case the app receives a "refresh token" that can be used to retrieve new short-lived access tokens as needed, without further manual user intervention. If you're using .NET, we recommend using the official Dropbox API v2 .NET SDK. When you use the official Dropbox API v2 .NET SDK, the SDK will handle the refresh process for you automatically, as long as you provide the necessary credentials (that is, the refresh token, the app key, and the app secret when not using PKCE). You can find examples of using this in C# here. In particular, there's the OauthBasic example which shows how to do this with the non-PKCE flow (for server-side apps) and the OAuthPKCE example which shows how to do this with the PKCE flow (for client-side apps).

  • kdss-ja's avatar
    kdss-ja
    Explorer | Level 3
    3 years ago

     

    After continuous effort, changed 127.0.0.1 to localhost to get it redirect working.

    Next Issue:

    still can get this to work, thanks in advance!

    //give all scope

    string[] scopeList = new string[6] { "files.metadata.read", "files.content.read", "account_info.read", "files.metadata.write", "files.content.write", "file_requests.write" };

    //succesfully acquire tokens
    var uid = await this.AcquireAccessToken(scopeList, IncludeGrantedScopes.None);

    HttpClient httpClient = new HttpClient();

    try
    {
    var config = new DropboxClientConfig("ArgusCE")
    {
    HttpClient = httpClient
    };

    Console.WriteLine("Attempting to try again with include_granted_scopes");
    await this.AcquireAccessToken(scopeList, IncludeGrantedScopes.User);

    client = new DropboxClient(AccessToken, ApiKey, ApiSecret, config);  //note accesstoken was acquired successfully

     

    //create new dropbox client but when call 

    await client.Files.ListFolderAsync(Path);

     

    also calling var full = await client.Users.GetCurrentAccountAsync(); fails?

    throws exception

     

    Response status code does not indicate success: 400 (Bad Request).

  • kdss-ja's avatar
    kdss-ja
    Explorer | Level 3
    3 years ago

    Narrowing it down

     

    //Client = new DropboxClient(AccessToken, ApiKey, ApiSecret, config); //this doesn't work throws exception
    Client = new DropboxClient(AccessToken, config); //this works

     

    Question will this AccessToken last for long time? 

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    3 years ago

    kdss-ja wrote:

    ...

    Question will this AccessToken last for long time? 


    Hi kdss-ja,

    If as long time, you mean 4 hours or so - Yes, otherwise - No. Doesn't matter how you gonna get your access token, it's always short lived.

    Read with some more care Greg's comments above! The Dropbox client object can manage the access token refresh for you. To be possible, you need to pass the refresh token too (for use in the refresh process), otherwise once the passed access token expired exception gonna be thrown. More precise directions how you can construct client objects can be seen here (non PKCE) and here (PKCE). Note that parameters don't exclude the refresh token there, as you are doing. Actually the access token can be removed everywhere (if suitable).

    Hope this sheds additional light.

  • kdss-ja's avatar
    kdss-ja
    Explorer | Level 3
    3 years ago

    Ok thanks I was just trying to follow the basic sample.

    I would suggest you update the samples to make them work, reading the doco is confusing.

    But this does work (again remove the other constructors or get them to throw appropriate exceptions)

    new DropboxClient(AccessToken, RefreshToken, ApiKey, ApiSecret, config);

     

    I'm only using drop box as simple way to share files between people (having trouble with the simple)

     

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!