Forum Discussion

Adi4's avatar
Adi4
Explorer | Level 3
2 years ago

Access Token

Hello !!
I use Dropbox API . I generate client to call method but after some time period it expire. Every time go to APP and generate Access token it not possible. It is possible for testing purpose. But when code go for production How to change Access Token. There is any code or method that generate Access Token dynamically. I use this code to make client

DbxRequestConfig config = DbxRequestConfig.newBuilder("dropbox/java-tutorial").build();
DbxClientV2 client = new DbxClientV2(config, accessToken);

In this I generate Access token after sometime and pass the access token value .

If their any other way to make client that generate Access Token dynamically please guide me
Thank You !!

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

    Adi4 wrote:

    ...

    If their any other way to make client that generate Access Token dynamically please guide me
    ...


    Hi Adi4,

    Access token is short lived only and expires in 4 hours or so. For long term access you need refresh token that doesn't expire automatic. For Java, there are examples in Java SDK. You can take a look here for PKCE OAuth flow that generates refresh token. It's simplified implementation and can be improved, of course. Credential object creation that replace raw access token can be seen here and its use (client object creation) here. 😉

    Hope this helps.

    • Adi4's avatar
      Adi4
      Explorer | Level 3

      Hi

      I have try and write this code to get Access Token 

      DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
      DbxAppInfo appInfoWithoutSecret = new DbxAppInfo(appKey);
      DbxPKCEWebAuth pkceWebAuth = new DbxPKCEWebAuth(requestConfig, appInfoWithoutSecret);

      // Start authorization with PKCE
      DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
      .withNoRedirect()
      .withTokenAccessType(TokenAccessType.OFFLINE)
      .build();

      String authorizeUrl = pkceWebAuth.authorize(webAuthRequest);
      System.out.println("1. Go to " + authorizeUrl);
      System.out.println("2. Click \"Allow\" (you might have to log in first).");
      System.out.println("3. Copy the authorization code.");

      // Read authorization code from console and trim
      System.out.print("Enter the authorization code here: ");
      Scanner scanner = new Scanner(System.in);
      String code = scanner.nextLine().trim();
      scanner.close();

      if (code == null || code.isEmpty()) {
      System.exit(1);
      }

      // Finish authorization and obtain the access token
      DbxAuthFinish token = null;
      try {
      token = pkceWebAuth.finishFromCode(code);
      } catch (DbxException e) {
      throw new RuntimeException(e);
      }
      String accessToken = token.getAccessToken();
      System.out.println("Access Token: " + accessToken);

      // You can use the accessToken to make API calls with the Dropbox SDK
      DbxClientV2 dbxClient = new DbxClientV2(requestConfig, accessToken);



      But it continously run After print this Statement.

      1. Go to https://www.dropbox.com/oauth2/authorize?token_access_type=offline&response_type=code&code_challenge_method=S256&client_id=70uguz533zsz15p&code_challenge=P101lG03EZGCnl3szeYcfOK51IOv2ZxNtW_d732PF8Q
      2. Click "Allow" (you might have to log in first).
      3. Copy the authorization code.

      Where I lack ?. It is not able to trim the code 
      Please help to get Access token

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

        😁 Adi4, Of course, your code will always executes regardless have you passed already OAuth or no!!! Be more careful - restructure your code better. You have one more error that will comes up once you fix current error.


        Здравко wrote:

        ...

        Access token is short lived only and expires in 4 hours or so. For long term access you need refresh token that doesn't expire automatic. ...


        In this context why do you keep access token only?! Where is the refresh token and where you put your credentials object?! 🙋 Again - take care - it wouldn't work without corrections. Just adding some additional code wouldn't make access token life longer! There is no such way - it'll be valid for no more 4 hours!!!

    • Adi4's avatar
      Adi4
      Explorer | Level 3

      I am little bit confused. How to implement.😒. But I will try

       

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,950 PostsLatest Activity: 25 minutes ago
352 Following

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 or Facebook.

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!