views:

58

answers:

1

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?

A: 

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);
}
emeraldjava