cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

OAuth2 one token for differet interfaces

OAuth2 one token for differet interfaces

thorty
Explorer | Level 3

Hello,

 

with APIv1 I have build an App wich runs in java on different machines. The App is linked with a speacial dropbox folder so that the app hase the same file basis on every machine.

 

How could I build this with the APIv2 without copy the token everytime on a new machine?

 

Now I have only one token to access my dropbox. If I generate one with the Java SDK I can use it. Now I switch to the other machine at work and the app does not know any token. So it will try to generate a new token. Now switch to the other machine and the token is not longer working. 😞

 

I hope you can understand my problem. Have you any Solution? 

 

Thanks

3 Replies 3

Greg-DB
Dropbox Staff
API v1 and API v2 should work the same way in this regard. You can use the same OAuth 2 access token from multiple instances of the same app, or you can create and use multiple different access tokens. Creating additional access tokens does not invalidate previous access tokens.

If something isn't working as expected, please share a sample request and unexpected response. (Be sure to redact the access token itself though.)

thorty
Explorer | Level 3

Thanks Greg,

 

"or you can create and use multiple different access tokens" - that would be great!

 

I use the Java API SDK. Can you tell my how I can get an existing token or generate an additional token? Whenever I creat a "new" token the older token does not working anymore and an "invalid access token" exception is thrown. 

 

This is my code to generate a new token:

 

public static String doAuth() {

        // Only display important log messages.
        Logger.getLogger("").setLevel(Level.WARNING);

        // Read app info file (contains app key and app secret)
        DbxAppInfo appInfo;
        appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);

        // Run through Dropbox API authorization process
        DbxRequestConfig requestConfig = new DbxRequestConfig("foo");
        DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
        
        
        DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
                .withNoRedirect()
                .withForceReapprove(Boolean.FALSE)
                .build();

        String authorizeUrl = webAuth.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: ");

        String code = null;
        try {
            code = new BufferedReader(new InputStreamReader(System.in)).readLine();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (code == null) {
            System.exit(1);
            return null;
        }
        code = code.trim();

        DbxAuthFinish authFinish;
        try {
            authFinish = webAuth.finishFromCode(code);
        } catch (DbxException ex) {
            System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
            System.exit(1);
            return null;
        }

        System.out.println("Authorization complete.");
        System.out.println("- User ID: " + authFinish.getUserId());
        System.out.println("- Access Token: " + authFinish.getAccessToken());

        // Save auth information to output file.
        DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.getAccessToken(), appInfo.getHost());
        File output = new File("token");
        try {
            DbxAuthInfo.Writer.writeToFile(authInfo, output);
            System.out.println("Saved authorization information to \"" + output.getCanonicalPath() + "\".");
        } catch (IOException ex) {
            System.err.println("Error saving to <auth-file-out>: " + ex.getMessage());
            System.err.println("Dumping to stderr instead:");
            try {
                DbxAuthInfo.Writer.writeToStream(authInfo, System.err);
            } catch (IOException ex1) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        return authFinish.getAccessToken();
    }

Greg-DB
Dropbox Staff

@thorty Using the DbxWebAuth flow you have is a valid way to get another access token, and that shouldn't revoke any previous tokens

 

Can you elaborate on the issue you're having with the previous token? E.g., please share the code and full output.

 

Make sure you're not mixing up the "authorization code" and "access token". The authorization code is single-use only, and can exchanged for an access token once. The resulting access token can be stored and re-used.

 

There are other ways tokens can be revoked too, so make sure you're not accidentally doing any of the following:

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    thorty Explorer | Level 3
What do Dropbox user levels mean?