Esempio n. 1
0
  private ArrayList<Post> FilteredFeedPosts() {
    Date daysAgo = new Date(currentTimeMillis() - 1000L * 60L * 60L * 24L * 10);

    Connection<Post> filteredFeed =
        facebookClient.fetchConnection(
            "me/feed", Post.class, Parameter.with("since", daysAgo), Parameter.with("limit", 100));
    out.println("***********beforehomefeed**********************************");

    return new ArrayList<Post>(filteredFeed.getData());
  }
  /**
   * Method that performs introspection on an AUTH string, and returns data as a String->String
   * hashmap.
   *
   * @param auth the authstring to query, as built by an auth impl.
   * @return the data from the introspect, in a map.
   * @throws IOException if anything goes wrong.
   */
  private Map<String, String> introspectAuth(String accesstoken) throws IOException {
    Map<String, String> results = new HashMap<String, String>();

    // create a fb client using the supplied access token
    FacebookClient client = new DefaultFacebookClient(accesstoken, Version.VERSION_2_5);

    try {
      // get back just the email, and name for the user, we'll get the id
      // for free.
      // fb only allows us to retrieve the things we asked for back in
      // FacebookAuth when creating the token.
      User userWithMetadata =
          client.fetchObject("me", User.class, Parameter.with("fields", "email,name"));

      results.put("valid", "true");
      results.put("email", userWithMetadata.getEmail());
      results.put("name", userWithMetadata.getName());
      results.put("id", "facebook:" + userWithMetadata.getId());

    } catch (FacebookOAuthException e) {
      results.clear();
      results.put("valid", "false");
    }

    return results;
  }
 public String postFeed(int account_index, String feed) {
   String accountToken = accounts.getData().get(account_index).getAccessToken();
   accountName = accounts.getData().get(account_index).getId();
   FacebookClient account = new DefaultFacebookClient(accountToken, Version.VERSION_2_4);
   FacebookType pubAcctMsg =
       account.publish("me/feed", FacebookType.class, Parameter.with("message", feed));
   return pubAcctMsg.getId().split("_")[1];
 }
 public String postPhotoFeed(int account_index, String feed, String image) {
   String accountToken = accounts.getData().get(account_index).getAccessToken();
   accountName = accounts.getData().get(account_index).getId();
   FacebookClient account = new DefaultFacebookClient(accountToken, Version.VERSION_2_4);
   FacebookType pubAcctMsg =
       account.publish(
           // F360 FORUM = 1401856530050018
           "me/photos",
           FacebookType.class,
           BinaryAttachment.with("cat.png", PhotoBytes.fetchBytesFromImage(image)),
           Parameter.with("message", feed));
   return pubAcctMsg.getId();
 }
Esempio n. 5
0
  private void facebookAuthorise() throws FacebookException {
    // I saved my session key to a .properties file for future use so the user doesn't keep have to
    // authorising my app!
    // auth.getSession returns JSON so we need to decode it just to grab the session key
    // http://wiki.developers.facebook.com/index.php/Auth.getSession
    // Here we pickup the session key and define the authToken we used above in facebookConnect()
    JSONObject json =
        new JSONObject(
            facebookClient.executeQuery(
                "auth.getSession", String.class, Parameter.with("auth_token", facebookAuthToken)));
    facebookSessionKey = json.getString("session_key");

    // An example call, you can literally use anything from the REST API
    Long uid = facebookClient.executeQuery("users.getLoggedInUser", facebookSessionKey);
    System.out.println("FB User ID: " + uid);
  }
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
      // THIS is for NOKIA application
      Logger.getLogger(FBPlacesServlet.class.getName())
          .log(Level.SEVERE, "Oops !!! Somebody called " + FBPlacesServlet.class.getName());

      if (HttpUtils.isEmptyAny(request, "latitude", "longitude", "distance")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      } else {
        String latitude = request.getParameter("latitude");
        String longitude = request.getParameter("longitude");
        String distance = request.getParameter("distance");
        int limit = NumberUtils.getInt(request.getParameter("limit"), 30);

        FacebookClient facebookClient =
            FacebookUtils.getFacebookClient(Commons.getProperty(Property.fb_app_token));

        String query = request.getParameter("q");
        JsonObject placesSearch = null;

        if (query != null && query.length() > 0) {
          placesSearch =
              facebookClient.fetchObject(
                  "search",
                  JsonObject.class,
                  Parameter.with("type", "place"),
                  Parameter.with("center", latitude + "," + longitude),
                  Parameter.with("distance", distance),
                  Parameter.with("q", query),
                  Parameter.with("limit", limit));
        } else {
          placesSearch =
              facebookClient.fetchObject(
                  "search",
                  JsonObject.class,
                  Parameter.with("type", "place"),
                  Parameter.with("center", latitude + "," + longitude),
                  Parameter.with("distance", distance),
                  Parameter.with("limit", limit));
        }

        JsonArray data = placesSearch.getJsonArray("data");

        ArrayList<Object> jsonArray = new ArrayList<Object>();
        String output = "";

        if (request.getParameter("version") != null
            && request.getParameter("version").equals("3")) {

          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            jsonObject.put("name", place.getString("name"));
            jsonObject.put("url", place.getString("id"));

            Map<String, String> desc = new HashMap<String, String>();
            if (place.has("category")) {
              desc.put("category", place.getString("category"));
            }
            JsonObject location = place.getJsonObject("location");
            Iterator<?> iter = location.sortedKeys();
            while (iter.hasNext()) {
              String next = (String) iter.next();
              if (!(next.equals("latitude") || next.equals("longitude"))) {
                desc.put(next, location.getString(next));
              }
            }
            jsonObject.put("desc", desc);
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("ResultSet", jsonArray);
          output = json.toString();

        } else if (request.getParameter("version") != null
            && request.getParameter("version").equals("2")) {

          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            jsonObject.put("name", place.getString("name"));
            jsonObject.put("desc", place.getString("id"));
            JsonObject location = place.getJsonObject("location");
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("ResultSet", jsonArray);
          output = json.toString();

        } else {
          // data
          for (int i = 0; i < data.length(); i++) {
            Map<String, Object> jsonObject = new HashMap<String, Object>();
            JsonObject place = (JsonObject) data.get(i);
            if (place.has("name")) {
              jsonObject.put("name", place.getString("name"));
            } else {
              jsonObject.put("name", place.getString("id"));
            }
            jsonObject.put("id", place.getString("id"));
            JsonObject location = place.getJsonObject("location");
            jsonObject.put("lat", MathUtils.normalizeE6(location.getDouble("latitude")));
            jsonObject.put("lng", MathUtils.normalizeE6(location.getDouble("longitude")));
            jsonArray.add(jsonObject);
          }

          JSONObject json = new JSONObject().put("data", jsonArray);
          output = json.toString();
        }

        out.println(output);
      }
    } catch (Exception ex) {
      Logger.getLogger(FBPlacesServlet.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
      if (ex instanceof FacebookOAuthException) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
      } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      }
    } finally {
      out.close();
    }
  }