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

Forum Discussion

env's avatar
env
Explorer | Level 3
3 years ago

retrieve access code automatically - javascript

Hey!
I'm trying to connect to Dropbox using Oauth and I cant figure out a way to retrieve the access code automatically.
I want that after I press connect in my site and the user press continue and then allow to retrieve the access code with no more user interaction' later I will use this access code to obtain access token.
can you please provide an example how to do that? there is an API call of dropbpx-sdk for that?
Thank you very much in advance!

9 Replies

  • env's avatar
    env
    Explorer | Level 3
    3 years ago

    Hey!
    I'm trying to connect to Dropbox using Oauth and I cant figure out a way to retrieve the access code automatically.
    I want that after I press connect in my site and the user press continue and then allow to retrieve the access code with no more user interaction' later I will use this access code to obtain access token.
    can you please provide an example how to do that? there is an API call of dropbpx-sdk for that?
    Thank you very much in advance!

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

    env wrote:

    ...
    can you please provide an example how to do that? there is an API call of dropbpx-sdk for that?
    ...


    Hi env,

    You didn't mention what SDK you are using. If you use Python, then an example can be seen here. If you use some other SDK you can adapt the same idea. 😉

    Good luck.

  • env's avatar
    env
    Explorer | Level 3
    3 years ago

    Sorry you right, I'm using javascript..

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

    It depends where are you using javascript. If you use browser session, better rely on Dropbox user session. In such a way you don't need any code or refresh token management. If you use javascript for standalone client side application or server application (node for instance), use something like my previous example. Both SDKs are very similar or similar enough at least.

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

    env I see you posted the same question in two threads, so I've combined them here in one.

     

    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. You can find more information in the OAuth Guide and authorization documentation.
     
    For examples of implementing this with the Dropbox JavaScript SDK, please refer to the examples included in the "examples" folder in the SDK. As long as you set the app key (a.k.a. client ID) and refresh token, like shown in this example for the JavaScript SDK, the SDK will actually handle refresh process for you automatically. The SDK will automatically catch expired access token errors and call the API to get a new short-lived access token when needed.

  • env's avatar
    env
    Explorer | Level 3
    3 years ago

    First of all Thank you very much for your respond!

    I cloned the javascript example from the link you brought.
    I get this error, which means it does not work on a Dropbox Business team - the app I have.
    should it work on Dropbox Business team app or just on a single Dropbox account? if so, can you provide an example for Dropbox Business team app?

  • env's avatar
    env
    Explorer | Level 3
    3 years ago

    Finally I would like to connect our product to the Dropbox Business app, And our clients will only need to put their user name and password for the Dropbox website, can I make it fully automated? it can be either in Java/Javascript. can you provide examples in both languages (Java - the flow start from getting the access code all the way to refresh the token once it is in our system, Javasript - start from getting the access code all the way to the callback function).
    Thank you in advance!

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

    env, As noted in the error message, you got, you need to specify which team member should be impersonated. While a personal account can represent single user only, team account represents least 3 members/users! That's why for team account you need to specify concrete member. During OAuth2 flow you get such ID. Keep that ID and take a look here how you can use it. You may need to specify default namespace too! By default (if nothing else specified) member home folder (not account root) is assumed as a root.

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

    env As Здравко noted, this error message is referring to specifying what account on the Business team to operate on behalf of. For reference, when using any "team scopes", the resulting access token is connected to an entire Dropbox Business team, not an individual account. So, when using a team-scoped access token to access user-specific endpoints, such as /2/users/get_current_account, you will need to specify which member of the team you want to operate on behalf of.
     
    To do this, you'd need to specify the 'Dropbox-API-Select-User' header (or 'Dropbox-API-Select-Admin' as needed/desired). The value should be the team_member_id for whichever member you wish to act on behalf of. (For example, you can find team member IDs by calling /2/team/members/get_info_v2 or /2/team/members/list_v2[/continue], or corresponding native method in an SDK.)

     

    (Alternatively, if you just want to connect to a particular account, you can disable any team scopes and get a new access token/refresh token without them. The access token without the team scopes will be specific to the particular account, Business or not, and so will not require the additional header. You can find more information on scopes in the OAuth Guide.)

     

    Dropbox does also offer an official Java SDK. You can find the documentation and examples here. Both of these SDKs support offline access with refresh tokens for unattended long-term access.

     

    To set the the 'Dropbox-API-Select-User' header in the JavaScript SDK, you would use the 'selectUser' option on the Dropbox object. To se it with the Java SDK, you would use DbxTeamClientV2.asMember.

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!