We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

u. katuhisa's avatar
u. katuhisa
New member | Level 2
9 years ago

Copy the authorization code.Where it is sufficient to copy?

  • u. katuhisa's avatar
    u. katuhisa
    New member | Level 2

    >>> https://www.dropbox.com/developers-v1/core/start/java

    If you run, you Copy the authorization code. Is displayed,
    Where this code may be paste?

    The complete code

    For those keeping score at home, here's the full source to this guide. Make sure to create a working-draft.txt file to get it to work fully. Also remember to insert your app key and secret.

    // Include the Dropbox SDK.
    import com.dropbox.core.*;
    import java.io.*;
    import java.util.Locale;
    
    public class Main {
        public static void main(String[] args) throws IOException, DbxException {
            // Get your app key and secret from the Dropbox developers website.
            final String APP_KEY = "INSERT_APP_KEY";
            final String APP_SECRET = "INSERT_APP_SECRET";
    
            DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
    
            DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",
                Locale.getDefault().toString());
            DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
    
            // Have the user sign in and authorize your app.
            String authorizeUrl = webAuth.start();
            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.");
            String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
    
            // This will fail if the user enters an invalid authorization code.
            DbxAuthFinish authFinish = webAuth.finish(code);
            String accessToken = authFinish.accessToken;
    
            DbxClient client = new DbxClient(config, accessToken);
    
            System.out.println("Linked account: " + client.getAccountInfo().displayName);
    
            File inputFile = new File("working-draft.txt");
            FileInputStream inputStream = new FileInputStream(inputFile);
            try {
                DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
                    DbxWriteMode.add(), inputFile.length(), inputStream);
                System.out.println("Uploaded: " + uploadedFile.toString());
            } finally {
                inputStream.close();
            }
    
            DbxEntry.WithChildren listing = client.getMetadataWithChildren("/");
            System.out.println("Files in the root path:");
            for (DbxEntry child : listing.children) {
                System.out.println("	" + child.name + ": " + child.toString());
            }
    
            FileOutputStream outputStream = new FileOutputStream("magnum-opus.txt");
            try {
                DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null,
                    outputStream);
                System.out.println("Metadata: " + downloadedFile.toString());
            } finally {
                outputStream.close();
            }
        }
    }
     
     
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    When using the OAuth 2 app authorization flow without a redirect URI, as is being done in the sample and screenshot you posted, the authorization code is that string in the text box on the Dropbox site, and needs to be copied into the app to finish the flow. 

    This line reads the authorization code from the console (where the user needs to paste it):

    String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

    And then this line passes it to the finish method to finish the app authorization flow:

    DbxAuthFinish authFinish = webAuth.finish(code);

    The documentation for that method can be found here:

    https://dropbox.github.io/dropbox-sdk-java/api-docs/v1.8.x/com/dropbox/core/DbxWebAuthNoRedirect.html#finish(java.lang.String)

    There's also an OAuth guide here that may serve as a useful reference:

    https://www.dropbox.com/developers/reference/oauth-guide

     

     

About Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 5 hours ago
323 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!