简体   繁体   中英

Unable to redirect to DropBox login page

I am integrating DropBox in my Java based application. The following piece of code, when executed in stand alone mode, works fine.

private String APP_KEY = "APP_KEY";
private String APP_SECRET = "APP_SECRET";
private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;

AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);


WebAuthInfo authInfo = session.getAuthInfo("callbackURL");

RequestTokenPair pair = authInfo.requestTokenPair;
String url = authInfo.url;
Desktop.getDesktop().browse(URL(url).toURI());
session.retrieveWebAccessToken(pair);

AccessTokenPair tokens = session.getAccessTokenPair();

In stand alone mode this piece of code redirects me to the DropBox log-in page, the user will authorize the app and then the control is back on to the page mentioned in the "callbackURL".

I use the same patch when i am actually running my site. I have a link saying "Link to DropBox now." On clicking the link, the control is passed to the server via a REST call to execute the above code. But it gets stuck at the line of code which says,

Desktop.getDesktop().browse(URL(url).toURI());

when all the parameters required to get the authInfo() are properly passed.

I am not able to determine where i am going wrong. Any help will be appreciated.

Since your is a web-application you cannot call Desktop.getDesktop() (which is meaningful only in the context of a desktop-application).

You should redirect the user to Dropbox (namely to the URL you receive in the statement authInfo.url ), using something like response.sendRedirect("callbackURL"); or using JavaScript on the client-side to set the href of a link.

Then, Dropbox will redirect the user back to your app (namely to the URL you specified as "callbackURL"), where you should have a servlet parse Dropbox's response ( more info here - take a look at the "RETURNS" section) and then acquire the _access_token_ ( as described here ).
(At that point you would probably want to store it somewhere (eg in the session-scope) along with the rest of the necessary data.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM