/** {@inheritDoc} */
  public Token refreshAccessToken(Token accessToken) {
    String accessTokenEndpoint = api.getAccessTokenEndpoint();
    if (accessTokenEndpoint.contains("?grant_type=")) {
      // handle the ugly case where the grant_type parameter is already hardcoded in the constant
      // url
      accessTokenEndpoint = accessTokenEndpoint.substring(0, accessTokenEndpoint.indexOf("?"));
    }
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), accessTokenEndpoint);

    switch (api.getAccessTokenVerb()) {
      case POST:
        request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
        request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
        // request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
        request.addBodyParameter(OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
        request.addBodyParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
        break;
      case GET:
      default:
        request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
        request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
        request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
        request.addQuerystringParameter(
            OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
        request.addQuerystringParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
    }

    Response response = request.send();
    return api.getAccessTokenExtractor().extract(response.getBody());
  }
Example #2
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));
  }
  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 #4
0
 public boolean postinTumblr(String username, Project project)
     throws SQLException, RemoteException {
   ResultSet result =
       connection
           .createStatement()
           .executeQuery(
               "select userToken,userSecret from users where username = \"" + username + "\"");
   Token token = new Token(result.getString(1), result.getString(2));
   OAuthRequest request =
       new OAuthRequest(
           Verb.POST, "http://api.tumblr.com/v2/blog/" + username + ".tumblr.com/post");
   request.addHeader("Accept", "application/json");
   request.addBodyParameter("type", "text");
   request.addBodyParameter("body", "Criei projeto " + project.getName());
   RMIServer.service.signRequest(token, request);
   org.scribe.model.Response response = request.send();
   System.out.println(response.getBody());
   Long postId = new JSONObject(response.getBody()).getJSONObject("response").getLong("id");
   System.out.println(postId);
   OAuthRequest request2 =
       new OAuthRequest(
           Verb.POST, "http://api.tumblr.com/v2/blog/" + username + ".tumblr.com/posts");
   request.addHeader("Accept", "application/json");
   request.addBodyParameter("type", "text");
   request.addBodyParameter("id", String.valueOf(project.getId()));
   RMIServer.service.signRequest(token, request2);
   org.scribe.model.Response response2 = request2.send();
   System.out.println(response2.getBody());
   String reblog_key =
       new JSONObject(response2.getBody()).getJSONObject("response").getString("reblog_key");
   System.out.println(
       "update projects set TumblrPostId = " + postId + " where id = " + project.getId());
   connection
       .createStatement()
       .execute(
           "update projects set TumblrPostId = "
               + postId
               + ",reblog_key = \""
               + reblog_key
               + "\" whessre id = "
               + project.getId());
   return true;
 }
Example #5
0
 public void likeProject(String username, int projectId) throws RemoteException, SQLException {
   ResultSet result =
       connection
           .createStatement()
           .executeQuery(
               "select userToken,userSecret from users where username = \"" + username + "\"");
   Token token = new Token(result.getString(1), result.getString(2));
   ResultSet result2 =
       connection
           .createStatement()
           .executeQuery("select TumblrPostId ,Reblog_key from projects where id = " + projectId);
   if (result2.getString(1) != null) {
     OAuthRequest request = new OAuthRequest(Verb.POST, "http://api.tumblr.com/v2/user/like");
     request.addHeader("Accept", "application/json");
     request.addBodyParameter("id", String.valueOf(result2.getLong(1)));
     request.addBodyParameter("reblog_key", result2.getString(2));
     RMIServer.service.signRequest(token, request);
     org.scribe.model.Response response = request.send();
     System.out.println(response.getBody());
   }
 }
  public void run() {
    try {
      System.out.println("Starting Twitter public stream consumer thread.");

      // Enter your consumer key and secret below
      OAuthService service =
          new ServiceBuilder()
              .provider(TwitterApi.class)
              .apiKey("qFiiPwXFjaiRsFAq5OSYHFT0f")
              .apiSecret("V9t7qXkbjglS76TKy6Xw9U2fukb1Nh1xmtKKR30kXR6noap9Qe")
              .build();

      // Set your access token
      Token accessToken =
          new Token(
              "66266917-XNT9WqkdFO8TQz6VQeCQtYEZIBdgpp82tUxCrIcIW",
              "2v4EMHESOz4yeajmjj8788mkGa2UOLAeUR1nrJ27tlSek");

      // Let's generate the request
      System.out.println("Connecting to Twitter Public Stream");
      OAuthRequest request = new OAuthRequest(Verb.POST, STREAM_URI);
      request.addHeader("version", "HTTP/1.1");
      request.addHeader("host", "stream.twitter.com");
      request.setConnectionKeepAlive(true);
      request.addHeader("user-agent", "Twitter Stream Reader");
      request.addBodyParameter(
          "track",
          "Bank of America, bofa, bofa_news, bofa_help, ROLB, BMB, Pingit, Ping-it, BarclaysMobileBanking, Barclays Live, Feature Store, Barclays, Barclaycard, BCS, BarclaysWealth, john mcfarlane, @Barclays, @Barclaysuk, hsbc, hsbc_press, citi, Citigroup Inc, RBS, Royal Bank Of Scotland, LLyods Bank, STAN, @StanChart, Santander, NatWest, Halifax, Tesco Bank"); // Set keywords you'd like to track here
      service.signRequest(accessToken, request);
      Response response = request.send();

      // Create a reader to read Twitter's stream
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getStream()));

      // Create a new file for raw tweets
      File file = new File("C:\\Users\\Striker\\Desktop\\Tweet Data\\raw_tweet.txt");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);

      String line;
      while ((line = reader.readLine()) != null) {
        latestTweet = line;
        bw.write(line);
        bw.newLine();
        tweetCount++;
        System.out.println(line);
      }

      bw.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   switch (api.getAccessTokenVerb()) {
     case POST:
       request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
       request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
       request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
       request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
       if (config.hasScope()) {
         request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
       }
       if (config.hasGrantType()) {
         request.addBodyParameter(OAuthConstants.GRANT_TYPE, config.getGrantType());
       }
       break;
     case GET:
     default:
       request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
       request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
       request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
       request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
       if (config.hasScope()) {
         request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
       }
       if (config.hasGrantType()) {
         request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, config.getGrantType());
       }
   }
   Response response = request.send();
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
  /* (non-Javadoc)
   * @see org.societies.api.internal.sns.ISocialConnector#post(java.lang.String)
   */
  @Override
  public void post(String activity) {

    JSONObject tweet = null;
    String res = null;
    try {
      tweet = new JSONObject(activity);
      OAuthRequest request = new OAuthRequest(Verb.POST, POST_TWEET_URL);
      if (tweet.has("status")) request.addBodyParameter("status", tweet.getString("status"));
      if (tweet.has("in_reply_to_status_id"))
        request.addBodyParameter("in_reply_to_status_id", tweet.getString("in_reply_to_status_id"));
      if (tweet.has("lat")) request.addBodyParameter("lat", tweet.getString("lat"));
      if (tweet.has("long")) request.addBodyParameter("long", tweet.getString("long"));
      if (tweet.has("place_id")) request.addBodyParameter("place_id", tweet.getString("place_id"));
      if (tweet.has("display_coordinates"))
        request.addBodyParameter("display_coordinates", tweet.getString("display_coordinates"));
      if (tweet.has("trim_user"))
        request.addBodyParameter("trim_user", tweet.getString("trim_user"));
      if (tweet.has("include_entities"))
        request.addBodyParameter("include_entities", tweet.getString("include_entities"));

      this.service.signRequest(twToken.getAccessToken(), request);
      Response response = request.send();

      res = response.getBody();

    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (res == null) System.out.println("failure");
    JSONObject resjson = null;
    try {
      resjson = new JSONObject(res);
      if (resjson.has("error")) {
        System.out.println(resjson.get("error"));
      }
      if (resjson.has("text")) {
        String resStatus = resjson.getString("text");
        if (resStatus.equalsIgnoreCase(tweet.getString("status"))) System.out.println("success");
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request =
       new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
   request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
   request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
   request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
   if (config.hasScope()) request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
   request.addBodyParameter(GRANT_TYPE, GRANT_TYPE_VALUE);
   Response response = request.send();
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
 /** {@inheritDoc} */
 public Token getAccessToken(final Token requestToken, final Verifier verifier) {
   final OAuthRequest request =
       new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   // PATCH : body parameters instead of request parameters
   request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
   request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
   request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
   request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
   if (config.hasScope()) request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
   // PATCH : + grant_type parameter
   request.addBodyParameter("grant_type", "authorization_code");
   final Response response = request.send();
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
  public void run() {
    try {
      System.out.println("Starting Twitter public stream consumer thread.");

      // Enter your consumer key and secret below
      OAuthService service =
          new ServiceBuilder()
              .provider(TwitterApi.class)
              .apiKey("0GglmCJSWdauygE0dPYVhRoYN")
              .apiSecret("aikSSCPcX1AOayVw98MKuPvalUKvPZhaESEJMawVGK9a9bfG0I")
              .build();

      // Set your access token
      Token accessToken =
          new Token(
              "863517241-dba29VzM7RaYMOvwFv03KOdkki6SVluRiRLqc1Ks",
              "cB8WS8duMCy4I30ZawZJFuv743jLdTWKLKkniMKfZOEQi");

      // Let's generate the request
      System.out.println("Connecting to Twitter Public Stream");
      OAuthRequest request = new OAuthRequest(Verb.POST, STREAM_URI);
      request.addHeader("version", "HTTP/1.1");
      request.addHeader("host", "stream.twitter.com");
      request.setConnectionKeepAlive(true);
      request.addHeader("user-agent", "Twitter Stream Reader");
      // request.addBodyParameter("track", "java,heroku,twitter"); // Set keywords you'd like to
      // track here
      request.addBodyParameter("follow", "4884271164"); // Set keywords you'd like to track here
      service.signRequest(accessToken, request);
      Response response = request.send();

      // Create a reader to read Twitter's stream
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getStream()));

      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
Example #12
0
  public synchronized String _request(String end, Verb verb, MaxCDNRequest body, Token token)
      throws SignatureException, Exception {
    OAuthService service =
        new ServiceBuilder().provider(MaxCDNApi.class).apiKey(key).apiSecret(secret).build();

    OAuthRequest request = new OAuthRequest(verb, this.MaxCDNrws_url + alias + end);
    request.addHeader("User-Agent", "Java MaxCDN API Client");
    if (verb == Verb.PUT || verb == Verb.POST) {
      for (int i = 0; i < body.names().length(); i++) {
        String key = (String) body.names().get(i);
        request.addBodyParameter(key, body.getString(key));
      }
    }
    service.signRequest((token == null) ? new Token("", "") : token, request);

    Response response = request.send();
    // Console.log(response.getBody());

    return response.getBody();
  }
Example #13
0
 /**
  * @param parameters
  * @param request
  */
 private void buildNormalPostRequest(Map<String, Object> parameters, OAuthRequest request) {
   for (Map.Entry<String, Object> entry : parameters.entrySet()) {
     request.addBodyParameter(entry.getKey(), String.valueOf(entry.getValue()));
   }
 }
 private void addBodyParameter(OAuthRequest request, String parameter, String value) {
   if (value != null) {
     request.addBodyParameter(parameter, value);
   }
 }