Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

Jagan Tirumalai's avatar
Jagan Tirumalai
Explorer | Level 4
2 years ago

Dropbox generate Auth Token thru java

Hi,

 

I am able to generate Access Token manually thru the dropbox website which is working fine. But expires after a few hours, Is there any method in Java that can generate access token programmatically? I would like to test this out so that we can start doing uploads and downloads by generating access tokens.

 

 

 

 

9 Replies

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    2 years ago

    Jagan Tirumalai, the example of the PKCE auth flow is meant to serve as a reference implementation. It's designed to help developers understand how PKCE works in practice but may not be suitable for all use cases or production environments without modification.

     

    We recommend reviewing the code and adapting it to fit your specific needs, including automating the auth flow.

  • Jagan Tirumalai's avatar
    Jagan Tirumalai
    Explorer | Level 4
    2 years ago

    Got it. Thank you once again for being patient in answering my questions.  The method getAccessToken() worked fine as expected.

     

    Is this part automated in pkceAuthorize.java ?  

     

    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.");

    System.out.print("Enter the authorization code here: ");

     

    Currently i am copying the authorizeUrl and pasting it into the web browser and copying back the authorization code and pasting it into my java application and then the application runs fine.

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    2 years ago

    Hi Jagan Tirumalai,

     

    You would need to use getter methods to retrieve the values of the respective properties. Following the example provided previously (for PKCE flow), it is returning a DbxAuthFinish object. That object contains getters for the properties that could be returned in the response — one of them being the access_token.

     

    In this specific case, the corresponding getter method to retrieve the access token would be getAccessToken().

     

    The following lines of code provide an example of one way the access_token could be retrieved:

     

     

     

    // Assign the DbxAuthFinish object to a variable
    DbxAuthFinish authFinish = authorize();
    
    // Use the getAccessToken() method to retrieve the access_token and save to a variable
    String accessToken = authFinish.getAccessToken();

     

     

     

     

    To retrieve the refresh_token, for offline access, you would use the getRefreshToken() method.

  • Jagan Tirumalai's avatar
    Jagan Tirumalai
    Explorer | Level 4
    2 years ago

    Hi,

     

    Thank you for your suggestion. The class PkceAuthorize.java doesn't generate or return any access_token. How do i go about using this class? 

     

    All i need is access_token to be generated without manual intervention, so that i can automate the dropbox .  Is there such a method in java dropbox api? 

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    2 years ago

    Hi Jagan Tirumalai,

     

    Based on the Auth URL you have provided, it looks like you are following a PKCE auth flow, which would explain why the suggestion to use DbxPKCEWebAuth is being made.

     

    If the intention is to use PKCE, we would recommend taking a look at this example instead.

  • Jagan Tirumalai's avatar
    Jagan Tirumalai
    Explorer | Level 4
    2 years ago

    Hi Greg-DB

     

    I have tried executing with ScopeAuthorize.java in my java program. When running the script its asking me to use DbxPKCEWebAuth instead of DbxWebAuth. So i have modified the code in ScopeAuthorize.java (local version) with DbxPKCEWebAuth. After changing with DbxPKCEWebAuth i am getting the following error when validating the access code. 

     

    Debug Stmts from ScopeAuthorize.java

    After authorize method

    Inside authorize method

    in Oauth2 flow1

    1. Go to https://www.dropbox.com/oauth2/authorize?token_access_type=online&scope=account_info.read&response_type=code&code_challenge_method=S256&client_id=4861qitn5qrgn07&code_challenge=V3LyeT-FoG7CgSw0f_h8qpmfEhu4q9JXchcMSVq4cro

    2. Click "Allow" (you might have to log in first).

    3. Copy the authorization code.

    Enter the authorization code here: 5dT79_yz4OAAAAAAAAACesA41RjYlsBmJaj9cl8HDkI

    After code.trim :5dT79_yz4OAAAAAAAAACesA41RjYlsBmJaj9cl8HDkI

    Before finishfromcode

    Error in DbxWebAuth.authorize: {"error": "invalid_grant", "error_description": "invalid code verifier"}

     

    I am not able to attach my java program and ScopeAuthorize.java (with my changes) to this forum. ScopeAuthorize.java is failing at line 133 which is given below.

    DbxAuthFinish authFinish = webAuth.finishFromCode(code);  Line 133

     

    which i have changed it to the following (note PKCEwebAuth instead of webAuth).

    System.out.println("Before finishfromcode"); --> its working up to this point

    DbxAuthFinish authFinish = PKCEwebAuth.finishFromCode(code); --> its failing here

    System.out.println("After finishfromcode");

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

    Jagan Tirumalai That "access code" is also known as the "authorization code". It should not be copied into the app's settings on the App Console; it should be copied back into the app itself. For example, refer to this line in the Java SDK example code that reads the authorization code back into the app so it can then finish the authorization flow.

  • Jagan Tirumalai's avatar
    Jagan Tirumalai
    Explorer | Level 4
    2 years ago

    Thank you very much for your response. I have generated an access code from oauth2. Its asking me to enter this code into CLIENTSDATA (which is the app i created in dropbox). I don't know where to enter this Access Code in ClientsData. Given below are the screenshots of the access code from OAUTH2 and ClientsData settings/permissions.  Where do i enter this access code in ClientsData settings/permissions?

     

     

    Spoiler
     

     

     

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    2 years ago

    Hi Jagan Tirumalai,

     

    Even though Dropbox issues short-lived access tokens, apps can get long-term access by requesting "offline" access, 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. Refresh tokens do not expire automatically and can be used repeatedly. You can find more information in the OAuth Guide and authorization documentation. There's a basic outline of processing this flow in this blog post which may serve as a useful example.

     

    For your use case, working with refresh tokens would be easier with our official Java SDK. Helper methods accept the refresh token, and manage this update of the short-lived access token for you.

     

    I hope you find this information helpful!

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!