public User updateUserDetails(String username, String displayName, String bio, MediaFile image)
      throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/user/" + username);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    if (displayName != null) {
      addStringPart(entity, "displayName", displayName);
    }
    if (bio != null) {
      addStringPart(entity, "bio", bio);
    }
    if (image != null) {
      entity.addPart("image", new ByteArrayBody(image.getData(), image.getFilename()));
    }

    request.addHeader("Content-Type", entity.getContentType().getValue());
    addMultipartEntity(request, entity);
    oauthSignRequest(request);

    Response response = request.send();

    final String repsonseBody = response.getBody();
    if (response.getCode() == 200) {
      return new UserParser().parseUserProfile(repsonseBody);
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public AccessToken authUser(
      String consumerKey, String username, String password, String consumerSecret)
      throws N0ticeException {
    log.info(
        "Attempting to auth user: "******", "
            + username
            + ", "
            + password
            + ", "
            + consumerSecret);
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/user/auth");
    addBodyParameter(request, "consumerkey", consumerKey);
    addBodyParameter(request, "username", username);
    addBodyParameter(request, "password", password);

    // Manually sign this request using the consumer secret rather than the access key/access
    // secret.
    addBodyParameter(request, "oauth_signature_method", "HMAC-SHA1");
    addBodyParameter(request, "oauth_version", "1.0");
    addBodyParameter(request, "oauth_timestamp", Long.toString(DateTimeUtils.currentTimeMillis()));
    final String effectiveUrl = request.getCompleteUrl() + "?" + request.getBodyContents();
    addBodyParameter(request, "oauth_signature", sign(effectiveUrl, consumerSecret));

    final Response response = request.send();
    final String responseBody = response.getBody();
    if (response.getCode() == 200) {
      return new UserParser().parseAuthUserResponse(responseBody);
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public String getUserFollowers() {
    OAuthRequest request = new OAuthRequest(Verb.GET, GET_FOLLOWERS_URL);
    this.service.signRequest(twToken.getAccessToken(), request);
    Response response = request.send();
    JSONObject res = null;
    JSONObject followers = new JSONObject();
    JSONArray followersList = new JSONArray();
    try {
      res = new JSONObject(response.getBody());
      JSONArray followersIDList = res.getJSONArray("ids");

      JSONObject other = null;

      for (int i = 0; i < followersIDList.length(); i++) {
        // System.out.println(friendsIDList.get(i).toString());

        other = getOtherProfileJson(followersIDList.get(i).toString());

        // System.out.println(other);
        if (!other.toString().contains("No user matches for specified terms"))
          followersList.put(other);
        followers.put("friends", followersList);
      }
    } catch (JSONException e) {
      response.getBody();
    }

    if (res != null)
      // return res.toJSONString();
      return followers.toString();
    else return null;
  }
    @Override
    protected Void doInBackground(String... verifier) {

      Verifier v = new Verifier(verifier[0]);

      // save this token for practical use.
      Token accessToken = service.getAccessToken(requestToken, v);

      OAuthRequest request =
          new OAuthRequest(Verb.GET, "http://api.openstreetmap.org/api/capabilities");
      service.signRequest(accessToken, request);
      Response response = request.send();

      Log.i("TakeABreak", response.getBody());

      SharedPreferences.Editor editor = settings.edit();

      editor.putString("accessToken", accessToken.getToken());
      editor.putString("accessSecret", accessToken.getSecret());

      // The requestToken is saved for use later on to verify the OAuth request.
      // See onResume() below
      editor.putString("requestToken", requestToken.getToken());
      editor.putString("requestSecret", requestToken.getSecret());

      editor.commit();
      mCallback.onSuccess();

      return null;
    }
 /**
  * Creates and sends a request to the Search API by term and location.
  *
  * <p>See <a href="http://www.yelp.com/developers/documentation/v2/search_api">Yelp Search API
  * V2</a> for more info.
  *
  * @param term <tt>String</tt> of the search term to be queried
  * @param location <tt>String</tt> of the location
  * @return <tt>String</tt> JSON Response
  */
 public String searchForBusinessesByLocation(String term, String location) {
   OAuthRequest request = createOAuthRequest(SEARCH_PATH);
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("location", location);
   request.addQuerystringParameter("limit", String.valueOf(SEARCH_LIMIT));
   return sendRequestAndGetResponse(request);
 }
Example #6
0
  public static void main(String[] args) throws Exception {

    OAuthService service =
        new ServiceBuilder().provider(TwitterApi.class).apiKey(apiKey).apiSecret(apiSecret).build();

    OAuthRequest request = new OAuthRequest(Verb.POST, URL);

    request.addBodyParameter("track", "wsop");

    Token t = new Token(token, tokenSecret);

    service.signRequest(t, request);

    Response response = request.send();

    JSONTokener jsonTokener = new JSONTokener(new InputStreamReader(response.getStream(), "UTF-8"));

    int i = 0;

    StringBuilder sb = new StringBuilder();

    while (i++ < 2) {

      try {

        JSONObject jsonObject = new JSONObject(jsonTokener);

        sb.append(new Tweet(jsonObject).getHTML() + "\n");
      } catch (JSONException ex) {
        throw new IOException("Got JSONException: " + ex.getMessage());
      }
    }

    System.out.println(HTMLCreator.createHTML(sb));
  }
  public Noticeboard editNoticeboard(
      String domain,
      String name,
      String description,
      Boolean moderated,
      Boolean featured,
      MediaFile cover)
      throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, urlBuilder.noticeBoard(domain));
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    addEntityPartParameter(entity, "name", name);
    addEntityPartParameter(entity, "description", description);
    addEntityPartParameter(
        entity, "moderated", moderated != null ? Boolean.toString(moderated) : null);
    addEntityPartParameter(
        entity, "featured", featured != null ? Boolean.toString(featured) : null);
    if (cover != null) {
      entity.addPart("cover", new ByteArrayBody(cover.getData(), cover.getFilename()));
    }

    // TODO implement
    /*
    if (endDate != null) {
    	addEntityPartParameter(entity, "endDate", ISODateTimeFormat.dateTimeNoMillis().print(new DateTime(endDate)));
    }
    if (cover != null) {
    	entity.addPart("cover", new ByteArrayBody(cover.getData(), cover.getFilename()));
    }

    StringBuilder supportedMediaTypesValue = new StringBuilder();
    Iterator<MediaType> supportedMediaTypesIterator = supportedMediaTypes.iterator();
    while(supportedMediaTypesIterator.hasNext()) {
    	supportedMediaTypesValue.append(supportedMediaTypesIterator.next());
    	if (supportedMediaTypesIterator.hasNext()) {
    		supportedMediaTypesValue.append(COMMA);
    	}
    }
    addEntityPartParameter(entity, "supportedMediaTypes", supportedMediaTypesValue.toString());

    if (group != null) {
    	addEntityPartParameter(entity, "group", group);
    }
    */

    request.addHeader("Content-Type", entity.getContentType().getValue());
    addMultipartEntity(request, entity);
    oauthSignRequest(request);

    Response response = request.send();

    final String responseBody = response.getBody();
    if (response.getCode() == 200) {
      return noticeboardParser.parseNoticeboardResult(responseBody);
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  protected void addOAuthParameter(OAuthRequest oAuthRequest, String key, String value) {

    if (oAuthRequest.getVerb() == Verb.GET) {
      oAuthRequest.addQuerystringParameter(key, value);
    } else if (oAuthRequest.getVerb() == Verb.POST) {
      oAuthRequest.addBodyParameter(key, value);
    }
  }
Example #9
0
 /**
  * Search with term and location.
  *
  * @param term Search term
  * @param latitude Latitude
  * @param longitude Longitude
  * @return JSON string response
  */
 public String search(String term, double latitude, double longitude) {
   OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("ll", latitude + "," + longitude);
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   return response.getBody();
 }
Example #10
0
 private void setTimeouts(OAuthRequest request) {
   if (connectTimeoutMs != null) {
     request.setConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS);
   }
   if (readTimeoutMs != null) {
     request.setReadTimeout(readTimeoutMs, TimeUnit.MILLISECONDS);
   }
 }
Example #11
0
 /**
  * Search with term and location.
  *
  * @param term Search term
  * @param latitude Latitude
  * @param longitude Longitude
  * @return JSON string response
  */
 public String search(String term, String location) {
   OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
   request.addQuerystringParameter("term", term);
   request.addQuerystringParameter("location", location);
   this.service.signRequest(this.accessToken, request);
   Response response = request.send();
   return response.getBody();
 }
Example #12
0
 /**
  * @param parameters
  * @param request
  */
 private void buildMultipartRequest(Map<String, Object> parameters, OAuthRequest request) {
   request.addHeader("Content-Type", "multipart/form-data; boundary=" + getMultipartBoundary());
   for (Map.Entry<String, Object> entry : parameters.entrySet()) {
     String key = entry.getKey();
     if (!key.equals("photo")) {
       request.addQuerystringParameter(key, String.valueOf(entry.getValue()));
     }
   }
 }
Example #13
0
 @Override
 public OAuthUser getUser(OAuthService service, Token accessToken) {
   OAuthRequest oauthRequest =
       new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
   service.signRequest(accessToken, oauthRequest);
   Response oauthResponse = oauthRequest.send();
   String body = oauthResponse.getBody();
   return parseInfos(body);
 }
Example #14
0
 /**
  * Call api endpoint
  *
  * @param verb http-method to use, like: GET, POST, PUT, DELETE, PATCH
  * @param url the api-url to call
  * @return the output of the api-call, can be a JSON-string
  */
 private String call(Verb verb, String url) {
   String urlEnd = url;
   if (!url.startsWith("/")) {
     urlEnd = "/" + url;
   }
   OAuthRequest request = new OAuthRequest(verb, "https://graph.facebook.com/v2.2" + urlEnd);
   request.addHeader("Authorization", "Bearer " + accessTokenString);
   Response response = request.send();
   return response.getBody();
 }
Example #15
0
  public String search(String location) {
    OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/business_review_search");
    request.addQuerystringParameter("location", location);
    request.addQuerystringParameter("term", "food");

    request.addQuerystringParameter("ywsid", "VM6cprFxivTjEhfQsoxKhQ");
    this.service.signRequest(this.accessToken, request);
    Response response = request.send();
    return response.getBody();
  }
 /**
  * Check: https://developer.linkedin.com/docs/fields/basic-profile
  *
  * @param accessToken
  * @return
  */
 public String getBasicProfile(String accessToken) {
   OAuthRequest oauthRequest =
       new OAuthRequest(
           Verb.GET,
           "https://api.linkedin.com/v1/people/~:(picture-url,email-address)?format=json");
   Token token = new Token(accessToken, apiSecret);
   oAuthService.signRequest(token, oauthRequest);
   Response oauthResponse = oauthRequest.send();
   String responseBody = oauthResponse.getBody();
   return responseBody;
 }
Example #17
0
 @Override
 public OAuthUser getUser(OAuthService service, Token accessToken) {
   OAuthRequest oauthRequest =
       new OAuthRequest(
           Verb.GET,
           "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address)");
   service.signRequest(accessToken, oauthRequest);
   Response oauthResponse = oauthRequest.send();
   String body = oauthResponse.getBody();
   return parseInfos(body);
 }
  public List<ModerationComplaint> getModerationComplaints(String id) throws N0ticeException {
    final OAuthRequest request = createOauthRequest(Verb.GET, urlBuilder.get(id) + "/flags");
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return moderationComplaintParser.parse(response.getBody());
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public Content authedGet(String id) throws N0ticeException {
    final OAuthRequest request = createOauthRequest(Verb.GET, urlBuilder.get(id));
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return searchParser.parseReport(response.getBody());
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public void closeNoticeboard(String domain) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, urlBuilder.closeNoticeboard(domain));
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public boolean voteInteresting(String id) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/" + id + "/vote/interesting");
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return true;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public int interestingVotes(String id) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.GET, apiUrl + "/" + id + "/votes/interesting");

    final Response response = request.send();

    if (response.getCode() == 200) {
      return searchParser.parseVotes(response.getBody());
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public boolean followUser(String username) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/user/" + username + "/follow");
    oauthSignRequest(request);

    final Response response = request.send();

    if (response.getCode() == 200) {
      return true;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public void closeModerationComplaint(String contentId, int flagId) throws N0ticeException {
    final OAuthRequest request =
        createOauthRequest(Verb.POST, urlBuilder.closeModerationComplaint(contentId, flagId));
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public Map<String, Map<String, String>> imageExif(String id) throws N0ticeException {
    final OAuthRequest request =
        createOauthRequest(Verb.GET, urlBuilder.get("image/" + id + "/exif"));
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return exifParser.parse(response.getBody());
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public boolean deleteReport(String id) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.DELETE, apiUrl + "/" + id);
    oauthSignRequest(request);

    final Response response = request.send();

    if (response.getCode() == 200) {
      return true;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public boolean moderate(String id, String notes, String action) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.POST, apiUrl + "/" + id + "/moderate/" + action);
    addBodyParameter(request, "notes", notes);
    oauthSignRequest(request);

    final Response response = request.send();
    if (response.getCode() == 200) {
      return true;
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
  public List<String> notifications(String username) throws N0ticeException {
    OAuthRequest request = createOauthRequest(Verb.GET, urlBuilder.userNotifications(username));
    oauthSignRequest(request);

    final Response response = request.send();

    if (response.getCode() == 200) {
      return searchParser.parseNotifications(response.getBody());
    }

    handleExceptions(response);
    throw new N0ticeException(response.getBody());
  }
 /*
  * Activities in Twitter is defined as tweets
  *
  * @see org.societies.api.internal.sns.ISocialConnector#getUserActivities()
  */
 public String getUserActivities() {
   OAuthRequest request = new OAuthRequest(Verb.GET, GET_TWEETS_URL);
   this.service.signRequest(twToken.getAccessToken(), request);
   Response response = request.send();
   JSONArray res = null;
   try {
     res = new JSONArray(response.getBody());
   } catch (JSONException e) {
     return response.getBody();
   }
   // System.out.println(res.toString());
   if (res != null) return res.toString();
   else return null;
 }
 public String getUserFriends() {
   OAuthRequest request = new OAuthRequest(Verb.GET, GET_FRIENDS_URL);
   this.service.signRequest(twToken.getAccessToken(), request);
   Response response = request.send();
   JSONObject res = null;
   try {
     res = new JSONObject(response.getBody());
   } catch (JSONException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   }
   if (res != null) return res.toString();
   else return null;
 }