简体   繁体   English

桌面Flickrj Java身份验证流程

[英]Desktop Flickrj Java Authentication Flow

I'm using the Flickrj API to log into flickr. 我正在使用Flickrj API登录到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. 对于READ来说,访问权限很好,但是当我需要WRITE访问权限才能为照片添加标签时,我似乎无法正确进行身份验证。

As i understand the basic auth flow 据我了解基本身份验证流程

  1. Get a frob 弄个东西
  2. Pass that frob requesting WRITE access, this returns a URL. 传递请求请求WRITE访问权限的frob,这将返回一个URL。
  3. Call the URL to recieve a flickr token 调用URL接收flickr令牌
  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? 如您所见-我陷入了第3步?

I found this code de.elmar_baumann.jpt.plugin.flickrupload.Authorization . 我发现此代码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. 步骤2之后,诀窍是让Java桌面应用程序打开浏览器窗口和对话框。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM