/** {@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()); }
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()); }
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(); } }