Esempio n. 1
0
  // Goes online to get user authentication from Flickr.
  public void getAuthentication() {
    AuthInterface authInterface = flickr.getAuthInterface();

    try {
      frob = authInterface.getFrob();
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      URL authURL = authInterface.buildAuthenticationUrl(Permission.WRITE, frob);

      // open the authentication URL in a browser
      open(authURL.toExternalForm());
    } catch (Exception e) {
      e.printStackTrace();
    }

    println("You have 15 seconds to approve the app!");
    int startedWaiting = millis();
    int waitDuration = 15 * 1000; // wait 10 seconds
    while ((millis() - startedWaiting) < waitDuration) {
      // just wait
    }
    println("Done waiting");

    try {
      auth = authInterface.getToken(frob);
      println("Authentication success");
      // This token can be used until the user revokes it.
      token = auth.getToken();
      // save it for future use
      saveToken(token);
    } catch (Exception e) {
      e.printStackTrace();
    }

    // complete authentication
    authenticateWithToken(token);
  }
Esempio n. 2
0
  // FLICKR AUTHENTICATION HELPER FUNCTIONS
  // Attempts to authneticate with a given token
  public void authenticateWithToken(String _token) {
    AuthInterface authInterface = flickr.getAuthInterface();

    // make sure the token is legit
    try {
      authInterface.checkToken(_token);
    } catch (Exception e) {
      println("Token is bad, getting a new one");
      getAuthentication();
      return;
    }

    auth = new Auth();

    RequestContext requestContext = RequestContext.getRequestContext();
    requestContext.setSharedSecret(secretKey);
    requestContext.setAuth(auth);

    auth.setToken(_token);
    auth.setPermission(Permission.WRITE);
    flickr.setAuth(auth);
    println("Authentication success");
  }