title: Dropbox Core API JAVA Authorization Code
body: <p>Using the dropbox <a href="https://www.dropbox.com/developers/core/start/java" rel="nofollow noreferrer">core api tutorial</a> I am able to upload a file.</p>
  
  <p>However, my question is an exact replica of this SO <a href="https://stackoverflow.com/questions/21831281/stuck-at-file-upload-download-app-in-dropbox-using-java?rq=1">post</a>--- That is, once I have my authorization code and comment out the user auth lines so that I dont have to manually re-authorize approval every time I use dropbox I get the following errors:</p>
  
  <pre><code>Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has already been used", "error": "invalid_grant"}
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has expired (within the last hour)", "error": "invalid_grant"}
  </code></pre>
  
  <p>I am positive I have the correct authorization code.</p>
  
  <p>I hope that I'm missing something, else whats the point of an API if you have to induce manual intervention every time you use it?</p>
  
  <p><strong>Edit:</strong> My Exact Code (keys have been scrambled)</p>
  
  <pre><code>import com.dropbox.core.*;
  import java.io.*;
  import java.util.Locale;
  
  public class DropboxUpload {
      public static void main(String[] args) throws IOException, DbxException {
          // Get your app key and secret from the Dropbox developers website.
          final String APP_KEY = "2po9b49whx74h67";
          final String APP_SECRET = "m98f734hnr92kmh";
  
          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();
  
          DbxAuthFinish authFinish = webAuth.finish("VtwxzitUoI8DDDLx0PlLut5Gjpw3");
          String accessToken = authFinish.accessToken;
  
          DbxClient client = new DbxClient(config, accessToken);
  
          System.out.println("Linked account: " + client.getAccountInfo().displayName);
  
          File inputFile = new File("/home/dropboxuser/Documents/test.txt");
          FileInputStream inputStream = new FileInputStream(inputFile);
          try {
              DbxEntry.File uploadedFile = client.uploadFile("/Public/test.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("test.txt");
          try {
              DbxEntry.File downloadedFile = client.getFile("/Public/test.txt", null,
                  outputStream);
              System.out.println("Metadata: " + downloadedFile.toString());
          } finally {
              outputStream.close();
          }
      }
  }
  </code></pre>