简体   繁体   中英

Desktop Flickrj Java Authentication Flow

I'm using the Flickrj API to log into flickr. For READ only access its fine, but I can't seem to correctly auth when i need WRITE access to add tags to photos.

As i understand the basic auth flow

  1. Get a frob
  2. Pass that frob requesting WRITE access, this returns a URL.
  3. Call the URL to recieve a flickr token
  4. Use the token in all subsequent requests

My code currently is

Flickr f = new Flickr(properties.getProperty(APIKEY),properties.getProperty(SECRET),t);
System.out.println(f.toString());

// 1 get a frob
AuthInterface authInterface = f.getAuthInterface();
String frob = authInterface.getFrob();
System.out.println("first frob "+frob);

// 2 get a request URL
URL url = f.getAuthInterface().buildAuthenticationUrl(Permission.WRITE,frob);
System.out.println(url.toString());

// 3 call the auth URL

// 4 get token
f.getAuthInterface().getToken(frob);

As you can see - i'm stuck on step 3?

I found this code de.elmar_baumann.jpt.plugin.flickrupload.Authorization . After step 2 the trick is to have the java desktop app open a browser window and a dialog. Once the user has logged in via the browser, they click the dialog so step four can be called and the token retrieved.

public boolean authenticate() {
    try {
        Flickr flickr = new Flickr("xx", "yy", new REST());
        Flickr.debugStream = false;
        requestContext = RequestContext.getRequestContext();
        authInterface  = flickr.getAuthInterface();
        frob           = authInterface.getFrob();
        token          = properties.getProperty(KEY_TOKEN);
        if (token == null) {
            authenticateViaWebBrowser();
        } else {
            auth = new Auth();
            auth.setToken(token);
        }
        requestContext.setAuth(auth);
        authenticated = true;
        return true;
    } catch (Exception ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, Bundle.getString("Auth.Error"));
    }
    return false;
}

private void authenticateViaWebBrowser() throws Exception {
    URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
    LargeMessagesDialog dlg = new LargeMessagesDialog(Bundle.getString("Auth.Info.GetToken.Browse", url.toExternalForm()));
    dlg.setVisible(true);
    Desktop.getDesktop().browse(url.toURI());
    JOptionPane.showMessageDialog(null, Bundle.getString("Auth.Info.GetToken.Confirm"));
    auth = authInterface.getToken(frob);
    token = auth.getToken();
    properties.setProperty(KEY_TOKEN, token);
}

I have a error, The code granted me no read permissions.. And I dont know why...
But otherwise I have a Frog and a Token.. And It works !!

        // Step 1) Get Frob
        AuthInterface ai = f.getAuthInterface();        
        String frob = ai.getFrob();
        System.out.println("frob: "+frob); //--> It Works !!

        // Step 2) URL With Permissions
        URL uc = ai.buildAuthenticationUrl(Permission.READ, frob);
        String request = uc.toExternalForm();
        uc.openConnection();

        // Step 3) Call URL
        System.out.println(request);
        URI uri = new URI(request);
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) 
        {
            desktop = Desktop.getDesktop();
        }

        if (desktop != null) 
        {
            desktop.browse(uri);   // Open Explorer to Confirm        
        }
        // Sleep until accepted in the explorer. After Press enter in Console
        BufferedReader infile = new BufferedReader ( new InputStreamReader (System.in) );
        String line = infile.readLine();

        // Step 4) Get a token
        Auth atoken = ai.getToken(frob); // Get a Token with a frob
        String stoken = atoken.getToken(); // Get a token like String
        System.out.println("Token: "+stoken);
        Auth au = ai.checkToken(stoken);   // Check token

        RequestContext.getRequestContext().setAuth(au);

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