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();
    }
  }
Exemplo n.º 2
0
  public static void main(String[] args) throws MalformedURLException {
    OAuthService service =
        new ServiceBuilder()
            .provider(FoursquareApi.class)
            .apiKey("FEGFXJUFANVVDHVSNUAMUKTTXCP1AJQD53E33XKJ44YP1S4I")
            .apiSecret("AYWKUL5SWPNC0CTQ202QXRUG2NLZYXMRA34ZSDW4AUYBG2RC")
            .build();
    Scanner in = new Scanner(System.in);

    System.out.println("=== Foursquare's OAuth Workflow ===");
    System.out.println();

    // Obtain the Request Token
    System.out.println("Fetching the Request Token...");
    Token requestToken = service.getRequestToken();
    System.out.println("Got the Request Token!");
    System.out.println();

    System.out.println("Now go and authorize Scribe here:");
    System.out.println(service.getAuthorizationUrl(requestToken));
    System.out.println("And paste the verifier here");
    System.out.print(">>");
    Verifier verifier = new Verifier(in.nextLine());
    System.out.println();

    // Trade the Request Token and Verfier for the Access Token
    System.out.println("Trading the Request Token for an Access Token...");
    Token accessToken = service.getAccessToken(requestToken, verifier);
    System.out.println("Got the Access Token!");
    System.out.println("(if your curious it looks like this: " + accessToken + " )");
    System.out.println();

    // Now let's go and ask for a protected resource!
    System.out.println("Now we're going to access a protected resource...");
    OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    service.signRequest(accessToken, request);
    Response response = request.send();
    System.out.println("Got it! Lets see what we found...");
    System.out.println();
    System.out.println(response.getBody());

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
  }
  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();
    }
  }