Serginho
Serginho

Reputation: 21

Dropbox authentication with CloudRail

I've tried integrating the Dropbox API using CloudRail but I've encountered a couple of issues:

  1. How to retrieve the access token via the CloudRail API
  2. What is the redirecURI when I'm coding in simple console application.
  3. Finally what I need to System.out.print within the onSuccess method.

Here is what I tried:


package com.cloudrail.userInterface;

import java.io.InputStream; import com.cloudrail.auth.UriRedirectListener; import com.cloudrail.auth.UriView; import com.cloudrail.auth.uriRedirect.HttpsServerRedirectListener; import com.cloudrail.auth.uriView.LocalUriCaller; import com.cloudrail.exception.CloudRailException; import com.cloudrail.userInterface.CloudDownload.DropboxDLResponseListener; public class CloudDownloadTest { final static String dropboxID = "someID"; // Put your Dropbox client id final static String dropboxSecret = "someSecret"; // Put your Dropbox client secret final static String dropboxCloudFilePath = "afile.txt"; // Put the path to the file that should be downloaded from Dropbox final static String dropboxDiskFilePath = "somePath/CopiedDPBFile"; // Put the path the Dropbox file should be downloaded to // I have questions about redirectURI - What is it how can I figure it out or take from somwehere. // Can I just give the hhtps://www.dropbox.com or it should be the site url where I integrate the API. final static String redirectURI = "soemURIString"; final static String state = Long.toHexString(Double.doubleToLongBits(Math.random())); // Random state to prevent CSRF // First argument is a dummy SSL context for local testing final static UriRedirectListener uriRedirectListener = new HttpsServerRedirectListener(Helper.getDummyContext(), 8080); final static UriView localUriCaller = new LocalUriCaller(); // Opens URLs in the local browser public static void main(String[] args) { // TODO Auto-generated method stub CloudDownload cloudDownload = new CloudDownload(dropboxID, redirectURI, dropboxSecret, state); cloudDownload.init_DropboxContentAPI_Auth(localUriCaller, uriRedirectListener, new AccessTokenCallback() { @Override public void onError(CloudRailException arg0) { // TODO Auto-generated method stub } @Override public void onToken(String arg0) { // TODO Auto-generated method stub // How to retrieve the access token with CloudRail cloudDownload.dropboxDL(dropboxCloudFilePath, "retrievedAccessToken", new DropboxDLResponseListener() { @Override public void onSuccess(InputStream file) { // TODO Auto-generated method stub // what I need to print out simple Hello work in console System.out.println("Hello"); } @Override public void onProgress(double percentFinished) { // TODO Auto-generated method stub } @Override public void onError(CloudRailException error) { // TODO Auto-generated method stub error.printStackTrace(); } }); } }); cloudDownload.start_DropboxContentAPI_Auth(); } }

Upvotes: 2

Views: 542

Answers (1)

Florian
Florian

Reputation: 712

To your first question: CloudRail actually does the job of providing the possibility to authenticate for you and your code is pretty close already. The AccessTokenCallback class you provide receives the access token in its onToken method as an argument after the user has successfully authenticated. That means you have to change your code as follows:

public void onToken(String arg0) {
    cloudDownload.dropboxDL(dropboxCloudFilePath, arg0, new DropboxDLResponseListener() { 
        ...

To your second question, the redirect URI for OAuth 2.0 is the URI the authentication server redirects to once authentication has succeeded. It will normally be a URL pointing to a special endpoint on your server. For a simple console application that runs locally, it suffices to redirect to localhost. Since your URIRedirectListener sets up a server to listen for such redirects on port 8080, you'll have to put

https://localhost:8080

as the redirect URI.

To address your third point, this should work as it is, assuming you fixed the other two things.

You have to remember of course to put valid values for client identifier and secret before you try your code.

Upvotes: 2

Related Questions