0

After reading the documentation to implement the Dropbox library in my project, I want the user, when confirming the authorization through the browser, his Access Token is passed directly to the app, without the user making copy/paste of the code. The documentation is unclear on how to use HttpServletRequest and HttpSession and therefore I get this error.

public class DropboxGenerateToken {

public static DbxAuthFinish authFinish;
public static DbxAppInfo appInfo;
public static DbxAuthInfo authInfo;
public static DbxRequestConfig config;
public static String ACCESS_TOKEN = "";



public static void main(String args[]) throws DbxException, FileNotFoundException, IOException {

    String accessToken = "";
    String userLocale = null;

    // Select a spot in the session for DbxWebAuth to store the CSRF token.
    HttpServletRequest request = null;
    HttpSession session = request.getSession(true);

    String sessionKey = "dropbox-auth-csrf-token";
    DbxSessionStore csrfTokenStore = new DbxStandardSessionStore(session, sessionKey);
    String redirectUri = "http://my-server.com/dropbox-auth-finish";


    DbxRequestConfig requestConfig = new DbxRequestConfig("text-edit/0.1", userLocale);
    DbxAppInfo appInfo = new DbxAppInfo("mycode", "mycode");
    DbxWebAuth auth = new DbxWebAuth(requestConfig, appInfo);


    // Build an auth request
    DbxWebAuth.Request authRequest = DbxWebAuth.newRequestBuilder()
            .withRedirectUri(redirectUri, csrfTokenStore)
            .build();

    String authorizeUrl = auth.authorize(authRequest);

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

    try {
        URL authenticationURL = new URL(authorizeUrl);
        Desktop.getDesktop().browse(authenticationURL.toURI());

    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    String code = JOptionPane.showInputDialog(DropboxGenerateToken.class, "Inserta el codigo de verificación.");
    System.out.println(code);
    code = code.trim();

    try {
        DbxAuthFinish authFinish = auth.finishFromCode(code);

        accessToken = authFinish.getAccessToken();

    } catch (Exception e) {

    }
}}

this is the error

Exception in thread "main" java.lang.NullPointerException
at login.DropboxGenerateToken.main(DropboxGenerateToken.java:61)

this line:HttpSession session = request.getSession(true);

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 3) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson Apr 17 '20 at 14:42
  • I followed this [link](https://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/com/dropbox/core/DbxWebAuth.html)to implement the use of the DopBox SDK in my Java desktop project. The NPE is done from this code. – Debora Carnevali Apr 18 '20 at 08:28
  • You've not followed my suggestions, or at least not edited the question with new information showing same, and not given any useful info in the latest comment. I'm not inclined to devote further time to this. – Andrew Thompson Apr 18 '20 at 09:57
  • my NPE does not depend on my code, but on the code published by DropBox, I only ask if someone has already had to deal with this. – Debora Carnevali Apr 18 '20 at 13:22
  • `HttpServletRequest request = null; HttpSession session = request.getSession(true);` In the 2nd code statement, `request` is `null`(as set *immediately before* that!).. – Andrew Thompson Apr 18 '20 at 14:41
  • [ Cross-linking for reference: https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Java-desktop-DbxWebAuth-with-withRedirectUri-NPE/td-p/411576 ] – Greg Apr 20 '20 at 15:52

0 Answers0