/** {@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());
  }
 /** {@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());
 }
 /** {@inheritDoc} */
 public Token getAccessToken(Token requestToken, Verifier verifier) {
   OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
   request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
   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());
   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("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());
   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());
   System.out.println("Access Token request is:" + request);
   Response response = request.send();
   System.out.println("Got the Access Token!");
   System.out.println("(if your curious here's the response: " + response.getBody() + " )");
   return api.getAccessTokenExtractor().extract(response.getBody());
 }
Exemplo n.º 6
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! :)");
  }
  /** {@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);
    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();
    System.out.println("Got the Refresh Token!");
    System.out.println("(if your curious here's the response: " + response.getBody() + " )");
    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();
    }
  }