close

First, instantiate a WebAuthSession using the appKeyPair for your application. Don't use an accessTokenPair, because you don't have one yet.

Second, do a .getAuthInfo(). You can provide it with a callback URL if you want, or plan to open the Dropbox url in a new window instead. 

Either way, you'll need some way of knowing when 

the user has finished authenticating. .getAuthInfo() will talk to Dropbox and return a WebAuthInfo object.

Third, examine the WebAuthInfo object returned above. It has a requestTokenPair field with an authentication pair that is currently invalid. Keep a temporary copy of this pair, 

as you'll need it to retrieve the permanent user authentication token in a minute, assuming 

the user completes the current authentication process. The object also has a url field.

Fourth, direct the user to that url field. This will be a dropbox site and 

the user will be given the option to authenticate themselves and grant your application access to their account.

Fifth, figure out some way of telling when the user is done authenticating. Either via the callback, 

or else by having a prompt where the user can click "ok" once they've finished and closed the Dropbox window.

Sixth, once the user is done, the requestTokenPair from step three will become valid for 5 minutes. 

You need to call .retrieveWebAccessToken() within that time period, and pass it the token pair. 

It will talk with Dropbox and then return a UID. Ignore this.

Seventh, and finally, now that oAuth authentication is complete, call .getAccessTokenPair(). 

It will return an AccessTokenPair containing a secret and a key. Save those. You'll need them in order to log the user back into Dropbox via your application in the future. 

If you don't save them, the user will have to re-authenticate each session.

                                                                                                                                     From dropbox forums,Josh


以上部分翻譯一下大概就能看得懂


Code:

 
import com.dropbox.client2.exception.DropboxException; 
import com.dropbox.client2.exception.DropboxServerException;
import com.dropbox.client2.session.Session; 
import com.dropbox.client2.session.WebAuthSession; 
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.AccessTokenPair; 
import com.dropbox.client2.DropboxAPI; 
import java.io.IOException; 
import java.util.List; 
public class CommandLineAuth { 
public static void main(String[] args) throws DropboxException, IOException 
 String server;
 AppKeyPair appKey; 
 AccessTokenPair accessToken; 
 if (args.length == 2) {
              appKey = new AppKeyPair(args[0], args[1]); accessToken = null;
 } else if (args.length == 4) {
   appKey = new AppKeyPair(args[0], args[1]); 
   accessToken = new AccessTokenPair(args[2], args[3]); 
 } else {
  System.err.println("Usage: COMMAND app-key 
                 app-secret [ access-token access-token-secret ]"); 
  System.exit(1); return;
 // ----------------------------------------------------
 // Do web-based OAuth
 WebAuthSession was = new WebAuthSession(appKey, Session.AccessType.DROPBOX);
 if (accessToken == null) { WebAuthSession.WebAuthInfo info = was.getAuthInfo();
 System.out.println("1. Go to: " + info.url);
 System.out.println("2. Allow access to this app."); 
 System.out.println("3. Press ENTER."); 
 while (System.in.read() != '\n') {} 
 String userId = was.retrieveWebAccessToken(info.requestTokenPair); 
 System.out.println("User ID: " + userId); 
 System.out.println("Access Key: " + was.getAccessTokenPair().key); 
 System.out.println("Access Secret " + was.getAccessTokenPair().secret); 
 } else { 
             was.setAccessTokenPair(accessToken); 
 DropboxAPI<WebAuthSession> api = new DropboxAPI<WebAuthSession>(was); 
 DropboxAPI.Account account = api.accountInfo(); System.out.println("User Name: " + account.displayName); } }

The code snippet you provided works for me. Can you try compiling and running the program below?

  1. Put all the "dropbox-sdk/lib/" JARs on your classpath when compiling and running.
  2. Pass in your consumer key and secret as the first two command-line arguments.
  3. Optionally, pass in your access token and secret as the next two command-line arguments.
 From dropbox forums,Kannan G.
 

另外eclipse命令參數列(command line argument)使用方法:
Project點右鍵 -> properties -> java editor -> Run/Debug Settings ->選取目標class -> Edit ->Argument
 


arrow
arrow

    匿名 發表在 痞客邦 留言(0) 人氣()