2

我正在使用Flickrj API 登录到 flickr。对于 READ 只能访问它的罚款,但当我需要 WRITE 访问权限来为照片添加标签时,我似乎无法正确验证。

据我了解基本的身份验证流程

  1. 得到一个青蛙
  2. 传递请求 WRITE 访问的 frob,这将返回一个 URL。
  3. 调用 URL 以接收 flickr 令牌
  4. 在所有后续请求中使用令牌

我的代码目前是

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

正如你所看到的 - 我被困在第 3 步?

4

2 回答 2

1

我找到了这段代码de.elmar_baumann.jpt.plugin.flickrupload.Authorization。在第 2 步之后,诀窍是让 java 桌面应用程序打开一个浏览器窗口和一个对话框。一旦用户通过浏览器登录,他们单击对话框,因此可以调用第四步并检索令牌。

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);
}
于 2010-07-30T13:49:33.320 回答
0

我有一个错误,代码没有授予我读取权限..而且我不知道为什么...
但是否则我有一个青蛙和一个令牌..它可以工作!

        // 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);
于 2012-03-28T15:37:48.370 回答