how to authenticate with Twitter4J? - java

I have some parameters stored in environment variables -- certainly those environment variables work with powershell.
Do I need slightly different parameters to authenticate with Twitter4J, perhaps?
powershell script output:
thufir#dur:~$
thufir#dur:~$ pwsh /home/thufir/powershell/helloPSTwitterAPI/twitter.ps1
RT #adamdriscoll: Today is the last day to sign up for the #Powershell #UniversalDashboard #udemy course at a discount rate. Check it out h…
RT #DirectoryRanger: Invoke-CommandAs
htt...
RT #adamdriscoll: Just added some #UniversalAutomation documentation about pre-defined variables in UA jobs. htt....
RT #adamdriscoll: #PowerShell #UniversalDashboard 2.8.2 is now available on the PowerShell Gallery. Lots of fixes, some improvements to Adm…
#psdevuk #adamdriscoll #psdbatools 👀
#adamdriscoll 🔥
#BillKindle #McDonalds #Wendys Sad, but that’s what I’m going to do next time. It should be ‘BigMac with Bacon Bits… htt...
I was excited to try out the new BigMac with Bacon... but horrible portion.. looks like cesar salad bacon bits...… htt...
#WindosNZ PSTwitterAPI? ;)
#Marioperator Thanks for the shoutout ❤️
RT #adamdriscoll: Nice! Financial charts for UD! htt.... #powershell htt...
#TomatoApp1 Constantly having to bind/unbind MiaoMiao device. And now the app won’t even open after trying reinstal… htt....
#adamdriscoll It shall get indexed and searchable in 15 minutes! I can just imagine your amazon shopping suggestions...
#adamdriscoll #LeeAlanBerg Pics or it didn’t happen
#SwiftOnSecurity #adbertram Did you end up finding a more elegant solution?
RT #racheldianeb: Had cake and wine tonight. 2 things I said I wouldn’t consume in Jan and would generally limit in 2020. It’s Jan 1st. So…
#adilio #sstranger Someone would probably be wrong.. 😝
#AndrewPlaTech #sstranger You have nothing to lose.. I mean, clearly I lost.. ;)
Someone’s mother has four sons. North, South and East. What is the name of the fourth son. Private message me the n… htt....
RT #EssentialSign_: For whoever needs this this evening. htt....
done
thufir#dur:~$
powershell script source:
thufir#dur:~$
thufir#dur:~$ cat /home/thufir/powershell/helloPSTwitterAPI/twitter.ps1
Import-Module PSTwitterAPI
#Set-TwitterOAuthSettings -ApiKey $env:ApiKey -ApiSecret $env:ApiSecret -AccessToken $env:AccessToken -AccessTokenSecret $env:AccessTokenSecret
Set-TwitterOAuthSettings -ApiKey $env:oAuthConsumerKey -ApiSecret $env:oAuthConsumerSecret -AccessToken $env:oAuthAccessToken -AccessTokenSecret $env:oAuthAccessTokenSecret
$TwitterStatuses = Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman'
Foreach ($status in $TwitterStatuses) {
Write-Host $status.text
}
Write-Host "done"
thufir#dur:~$
java crash:
thufir#dur:~/java/helloTwitter4J$
thufir#dur:~/java/helloTwitter4J$ gradle clean run
> Task :run FAILED
Jan. 29, 2020 4:16:31 P.M. helloTwitter4J.App runQuery
INFO: {oAuthAccessToken=abc, oAuthConsumerKey=def, oAuthConsumerSecret=ghi, oAuthAccessTokenSecret=jkl}
Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for details
at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:201)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1966)
at twitter4j.TwitterImpl.search(TwitterImpl.java:293)
at helloTwitter4J.App.runQuery(App.java:52)
at helloTwitter4J.App.main(App.java:60)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/home/thufir/.sdkman/candidates/java/12.0.1-zulu/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
4 actionable tasks: 4 executed
thufir#dur:~/java/helloTwitter4J$
java source:
package helloTwitter4J;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class App {
private static final Logger log = Logger.getLogger(App.class.getName());
private final Properties properties = new Properties();
private void loadProperties() throws InvalidPropertiesFormatException, IOException {
properties.loadFromXML(App.class.getResourceAsStream("/twitter.xml"));
log.fine(properties.toString());
Set<Object> keySet = properties.keySet();
String key = null;
String value = null;
for (Object obj : keySet) {
key = obj.toString();
value = System.getenv(key);
log.fine(key + value);
properties.setProperty(key, value);
}
}
private void runQuery() throws TwitterException, InvalidPropertiesFormatException, IOException {
loadProperties();
log.info(properties.toString()); //this matches what powershell uses
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setDebugEnabled(true)
.setOAuthConsumerKey(properties.getProperty("oAuthConsumerKey"))
.setOAuthConsumerSecret(properties.getProperty("oAuthConsumerSecret"))
.setOAuthAccessToken(properties.getProperty("oAuthAccessToken"))
.setOAuthAccessTokenSecret(properties.getProperty("oAuthAccessTokenSecret"));
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("source:twitter4j yusukey");
QueryResult result = twitter.search(query);
// for (Status status : result.getTweets()) {
// log.info("#" + status.getUser().getScreenName() + ":" + status.getText());
// }
}
public static void main(String[] args) throws InvalidPropertiesFormatException, IOException, TwitterException {
new App().runQuery();
}
}
I've also printed the properties file to the console and it looks correct. Certainly the variables are getting picked up, I'm just not entirely sure they're what Twitter4J requires. Perhaps a specific twitter.properties file would help.
whoops, need to actually build the factory properly:
https://www.dummies.com/web-design-development/mobile-apps/how-to-make-a-configurationbuilder-to-talk-to-the-twitter-server-with-your-android-app/

working hello world type code:
package helloTwitter4J;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class App {
private static final Logger log = Logger.getLogger(App.class.getName());
private Properties loadProperties() throws IOException {
Properties properties = new Properties();
properties.loadFromXML(App.class.getResourceAsStream("/twitter.xml"));
log.fine(properties.toString());
Set<Object> keySet = properties.keySet();
String key = null;
String value = null;
for (Object obj : keySet) {
key = obj.toString();
value = System.getenv(key);
log.fine(key + value);
properties.setProperty(key, value);
}
return properties;
}
private TwitterFactory configTwitterFactory() throws IOException {
Properties properties = loadProperties();
log.info(properties.toString()); //this matches what powershell uses
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setDebugEnabled(true)
.setOAuthConsumerKey(properties.getProperty("oAuthConsumerKey"))
.setOAuthConsumerSecret(properties.getProperty("oAuthConsumerSecret"))
.setOAuthAccessToken(properties.getProperty("oAuthAccessToken"))
.setOAuthAccessTokenSecret(properties.getProperty("oAuthAccessTokenSecret"));
TwitterFactory twitterFactory = null;
twitterFactory = new TwitterFactory(configurationBuilder.build());
return twitterFactory;
}
private void getHomeTimeLine() throws TwitterException, IOException {
Twitter twitter = configTwitterFactory().getInstance();
List<Status> statuses = null;
statuses = twitter.getHomeTimeline();
System.out.println("Showing home timeline.");
if (statuses != null) {
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":"
+ status.getText());
}
}
}
public static void main(String[] args) throws TwitterException, IOException {
new App().getHomeTimeLine();
}
}
also see https://github.com/nisrulz/twitterbot-java/blob/master/src/github/nisrulz/bot/TwitterBot.java

Related

Google Dataproc API (through Java) does not submit Job to cluster

I was trying to get this section of code to submit a Hadoop job request based on this code sample:
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.dataproc.v1.Job;
import com.google.cloud.dataproc.v1.JobControllerClient;
import com.google.cloud.dataproc.v1.JobControllerSettings;
import com.google.cloud.dataproc.v1.JobMetadata;
import com.google.cloud.dataproc.v1.JobPlacement;
import com.google.cloud.dataproc.v1.SparkJob;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SubmitJob {
public static void submitJob() throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String region = "your-project-region";
String clusterName = "your-cluster-name";
submitJob(projectId, region, clusterName);
}
public static void submitJob(
String projectId, String region, String clusterName)
throws IOException, InterruptedException {
String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region);
// Configure the settings for the job controller client.
JobControllerSettings jobControllerSettings =
JobControllerSettings.newBuilder().setEndpoint(myEndpoint).build();
// Create a job controller client with the configured settings. Using a try-with-resources
// closes the client,
// but this can also be done manually with the .close() method.
try (JobControllerClient jobControllerClient =
JobControllerClient.create(jobControllerSettings)) {
// Configure cluster placement for the job.
JobPlacement jobPlacement = JobPlacement.newBuilder().setClusterName(clusterName).build();
// Configure Spark job settings.
HadoopJob hadJob =
HadoopJob.newBuilder()
.setMainClass("my jar file")
.addArgs("input")
.addArgs("output")
.build();
Job job = Job.newBuilder().setPlacement(jobPlacement).setHadoopJob(hadJob).build();
// Submit an asynchronous request to execute the job.
OperationFuture<Job, JobMetadata> submitJobAsOperationAsyncRequest =
jobControllerClient.submitJobAsOperationAsync(projectId, region, job);
// THIS IS WHERE IT SEEMS TO TIMEOUT VVVVVVVV
Job response = submitJobAsOperationAsyncRequest.get();
// Print output from Google Cloud Storage.
Matcher matches =
Pattern.compile("gs://(.*?)/(.*)").matcher(response.getDriverOutputResourceUri());
matches.matches();
Storage storage = StorageOptions.getDefaultInstance().getService();
Blob blob = storage.get(matches.group(1), String.format("%s.000000000", matches.group(2)));
System.out.println(
String.format("Job finished successfully: %s", new String(blob.getContent())));
} catch (ExecutionException e) {
// If the job does not complete successfully, print the error message.
System.err.println(String.format("submitJob: %s ", e.getMessage()));
}
}
}
When running this sample, the code seems to timeout on Job response = submitJobAsOperationAsyncRequest.get(), and the Job is never submitted to my Google Cloud. I've checked all my project, region, and cluster names and I'm sure that is not the issue. I also have the following dependencies installed for the sample:
jar files
I believe I am not missing any .jar files.
Any suggestions? I appreciate any and all help.

Working with twitter API

I'm trying to create a small website which takes user's twitter username and retrieve the basic information from it like: screen name, created date, number of followers, etc... and display it. But I couldn't find a new helpful working tutorial or example of how to work with twitter API. Can someone please recommend something for me or just give some instructions of how should I start and work please? I prefer it to be done with Java or PHP.
Also I have a problem, when creating my application access key in twitter, I can't create the access token! the button is not shown! how can I solve this?
You Can use below code-
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterSearch {
private static final String CATEGORY = "Mobile";
private static final String SOURCE = "Twitter";
public static void main(String ...args) throws TwitterException, IOException
{
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TwitterConfig.OATH_CONSUMER_KEY)
.setOAuthConsumerSecret(TwitterConfig.OATH_CONSUMER_SECRET)
.setOAuthAccessToken(TwitterConfig.OATH_ACCESS_TOKEN)
.setOAuthAccessTokenSecret(TwitterConfig.OATH_ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
System.out.println("Product to be searched " + args[0]);
Query query = new Query(args[0]);
query.setLang("en");
query.setLocale("en_IN");
query.setCount(100);
QueryResult result = twitter.search(query);
System.out.println("Output File "+ args[1]);
System.out.println(result.getTweets());
for(Status tweet : result.getTweets())
{
System.out.println(tweet.getText());
}
}
}
result.getTweets() return a List, and you can iterate over that list and can print tweet.getText() to print tweet.
Use this maven dependency-
<dependency><groupId>org.twitter4j</groupId><artifactId>twitter4j-core</artifactId><version>[4.0,)</version></dependency>
Refer below link for keys-
https://dev.twitter.com/oauth/overview

With Elastic Beanstalk, can I determine programmatically if I'm on the leader node?

I have some housekeeping tasks within an Elastic Beanstalk Java application running on Tomcat, and I need to run them every so often. I want these tasks run only on the leader node (or, more correctly, on a single node, but the leader seems like an obvious choice).
I was looking at running cron jobs within Elastic Beanstalk, but it feels like this should be more straightforward than what I've come up with. Ideally, I'd like one of these two options within my web app:
Some way of testing within the current JRE whether or not this server is the leader node
Some some way to hit a specific URL (wget?) to trigger the task, but also restrict that URL to requests from localhost.
Suggestions?
It is not possible, by design (leaders are only assigned during deployment, and not needed on other contexts). However, you can tweak and use the EC2 Metadata for this exact purpose.
Here's an working example about how to achieve this result (original source). Once you call getLeader, it will find - or assign - an instance to be set as a leader:
package br.com.ingenieux.resource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.apache.commons.io.IOUtils;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.CreateTagsRequest;
import com.amazonaws.services.ec2.model.DeleteTagsRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.Tag;
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk;
import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
#Path("/admin/leader")
public class LeaderResource extends BaseResource {
#Inject
AmazonEC2 amazonEC2;
#Inject
AWSElasticBeanstalk elasticBeanstalk;
#GET
public String getLeader() throws Exception {
/*
* Avoid running if we're not in AWS after all
*/
try {
IOUtils.toString(new URL(
"http://169.254.169.254/latest/meta-data/instance-id")
.openStream());
} catch (Exception exc) {
return "i-FFFFFFFF/localhost";
}
String environmentName = getMyEnvironmentName();
List<Instance> environmentInstances = getInstances(
"tag:elasticbeanstalk:environment-name", environmentName,
"tag:leader", "true");
if (environmentInstances.isEmpty()) {
environmentInstances = getInstances(
"tag:elasticbeanstalk:environment-name", environmentName);
Collections.shuffle(environmentInstances);
if (environmentInstances.size() > 1)
environmentInstances.removeAll(environmentInstances.subList(1,
environmentInstances.size()));
amazonEC2.createTags(new CreateTagsRequest().withResources(
environmentInstances.get(0).getInstanceId()).withTags(
new Tag("leader", "true")));
} else if (environmentInstances.size() > 1) {
DeleteTagsRequest deleteTagsRequest = new DeleteTagsRequest().withTags(new Tag().withKey("leader").withValue("true"));
for (Instance i : environmentInstances.subList(1,
environmentInstances.size())) {
deleteTagsRequest.getResources().add(i.getInstanceId());
}
amazonEC2.deleteTags(deleteTagsRequest);
}
return environmentInstances.get(0).getInstanceId() + "/" + environmentInstances.get(0).getPublicIpAddress();
}
#GET
#Produces("text/plain")
#Path("am-i-a-leader")
public boolean isLeader() {
/*
* Avoid running if we're not in AWS after all
*/
String myInstanceId = null;
String environmentName = null;
try {
myInstanceId = IOUtils.toString(new URL(
"http://169.254.169.254/latest/meta-data/instance-id")
.openStream());
environmentName = getMyEnvironmentName();
} catch (Exception exc) {
return false;
}
List<Instance> environmentInstances = getInstances(
"tag:elasticbeanstalk:environment-name", environmentName,
"tag:leader", "true", "instance-id", myInstanceId);
return (1 == environmentInstances.size());
}
protected String getMyEnvironmentHost(String environmentName) {
return elasticBeanstalk
.describeEnvironments(
new DescribeEnvironmentsRequest()
.withEnvironmentNames(environmentName))
.getEnvironments().get(0).getCNAME();
}
private String getMyEnvironmentName() throws IOException,
MalformedURLException {
String instanceId = IOUtils.toString(new URL(
"http://169.254.169.254/latest/meta-data/instance-id"));
/*
* Grab the current environment name
*/
DescribeInstancesRequest request = new DescribeInstancesRequest()
.withInstanceIds(instanceId)
.withFilters(
new Filter("instance-state-name").withValues("running"));
for (Reservation r : amazonEC2.describeInstances(request)
.getReservations()) {
for (Instance i : r.getInstances()) {
for (Tag t : i.getTags()) {
if ("elasticbeanstalk:environment-name".equals(t.getKey())) {
return t.getValue();
}
}
}
}
return null;
}
public List<Instance> getInstances(String... args) {
Collection<Filter> filters = new ArrayList<Filter>();
filters.add(new Filter("instance-state-name").withValues("running"));
for (int i = 0; i < args.length; i += 2) {
String key = args[i];
String value = args[1 + i];
filters.add(new Filter(key).withValues(value));
}
DescribeInstancesRequest req = new DescribeInstancesRequest()
.withFilters(filters);
List<Instance> result = new ArrayList<Instance>();
for (Reservation r : amazonEC2.describeInstances(req).getReservations())
result.addAll(r.getInstances());
return result;
}
}
You can keep a secret URL (a long URL is un-guessable, almost as safe as a password), hit this URL from somewhere. On this you can execute the task.
One problem however is that if the task takes too long, then during that time your server capacity will be limited. Another approach would be for the URL hit to post a message to the AWS SQS. The another EC2 can have a code which waits on SQS and execute the task. You can also look into http://aws.amazon.com/swf/
Another approach if you're running on the Linux-type EC2 instance:
Write a shell script that does (or triggers) your periodic task
Leveraging the .ebextensions feature to customize your Elastic Beanstalk instance, create a container command that specifies the parameter leader_only: true -- this command will only run on an instance that is designated the leader in your Auto Scaling group
Have your container command copy your shell script into /etc/cron.hourly (or daily or whatever).
The result will be that your "leader" EC2 instance will have a cron job running hourly (or daily or whatever) to do your periodic task and the other instances in your Auto Scaling group will not.

incompatible type and can not find symbol in twitter4j?

this is what is the code and i get the problem in marked lines(BOLD), i think it is because of the jar version but i am not sure about this. if this is because of jar version please do let me know the right one.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class NamexTweet {
private final static String CONSUMER_KEY = "xxxxxxxxxxxxx";
private final static String CONSUMER_KEY_SECRET = "yyyyyyyyyyyyyyy";
public void start() throws TwitterException, IOException {
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
**RequestToken requestToken = twitter.getOAuthRequestToken();**
System.out.println("Authorization URL: \n"
+ requestToken.getAuthorizationURL());
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
try {
System.out.print("Input PIN here: ");
String pin = br.readLine();
**accessToken = twitter.getOAuthAccessToken(requestToken, pin);**
} catch (TwitterException te) {
System.out.println("Failed to get access token, caused by: "
+ te.getMessage());
System.out.println("Retry input PIN");
}
}
System.out.println("Access Token: " + accessToken.getToken());
System.out.println("Access Token Secret: "
+ accessToken.getTokenSecret());
twitter.updateStatus("hi.. im updating this using Namex Tweet for Demo");
}
public static void main(String[] args) throws Exception {
new NamexTweet().start();// run the Twitter client
}
}
Make sure the jar is actually in the build path (if I knew your IDE I might have given more concrete instructions).
If this doesn't solve the problem, search these classes and methods in that jar. If they're there - try to perform step 1 above better... If it's not there - then you have the wrong jar.
twitter4j 2.2.4 is the reliable version, which can be used.
You have to attach following libraries:
twitter4j-core-4.0.4.jar
twitter4j-stream-4.0.4.jar

How to programmatically put data to the Google Appengine database from remote executable?

I would like to pre-fill and periodically put data to the Google Appengine database.
I would like to write a program in java and python that connect to my GAE service and upload data to my database.
How can I do that?
Thanks
Please use RemoteAPI for doing this programmatically.
In python, you can first configure the appengine_console.py as described here
Once you have that, you can launch and write the following commands in the python shell:
$ python appengine_console.py yourapp
>>> import yourdbmodelclassnamehere
>>> m = yourmodelclassnamehere(x='',y='')
>>> m.put()
And here is code from the java version which is self explanatory (directly borrowed from the remote api page on gae docs):
package remoteapiexample;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;
import java.io.IOException;
public class RemoteApiExample {
public static void main(String[] args) throws IOException {
String username = System.console().readLine("username: ");
String password =
new String(System.console().readPassword("password: "));
RemoteApiOptions options = new RemoteApiOptions()
.server("<your app>.appspot.com", 443)
.credentials(username, password);
RemoteApiInstaller installer = new RemoteApiInstaller();
installer.install(options);
try {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
System.out.println("Key of new entity is " +
ds.put(new Entity("Hello Remote API!")));
} finally {
installer.uninstall();
}
}
}

Categories