List of usage examples for twitter4j.auth AccessToken AccessToken
public AccessToken(String token, String tokenSecret)
From source file:org.minesales.infres6.narvisAPITwiterConsole.communications.twitter.AccessTwitter.java
public static Twitter loadAccessTwitter() { String token = "2754794936-VRDsXE6ZxnBQeUTbprYHGEOVQcYJc1mvyNaBfyC"; String tokenSecret = "Sb7nwzWtlmOw6CRcqRoSlXkvonZGXiLTOp9sBAlv4fDOy"; AccessToken accessToken = new AccessToken(token, tokenSecret); TwitterFactory factory = new TwitterFactory(); Twitter twitter = factory.getInstance(); twitter.setOAuthConsumer("CkQjh66TbG4FYfry1OTrTb4tS", "v8vbUPtFAkIfSUN2KICf8GDEZ91fumVuY5mzMAmT84Ag2bVnvD"); twitter.setOAuthAccessToken(accessToken); return twitter; }
From source file:org.mule.twitter.TwitterConnector.java
License:Open Source License
/** * Connects to Twitter/*from w w w .j a va 2 s . c o m*/ * @param accessKey The access key provided by Twitter * @param accessSecret The access secret provided by Twitter * @throws ConnectionException when the connection fails */ @Connect public void connect(@ConnectionKey String accessKey, String accessSecret) throws ConnectionException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setUseSSL(useSSL); cb.setHttpProxyHost(proxyHost); cb.setHttpProxyPort(proxyPort); cb.setHttpProxyUser(proxyUsername); cb.setHttpProxyPassword(proxyPassword); HttpClientHiddenConstructionArgument.setUseMule(true); twitter = new TwitterFactory(cb.build()).getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); if (accessKey != null) { twitter.setOAuthAccessToken(new AccessToken(accessKey, accessSecret)); setAccessToken(accessKey); setAccessTokenSecret(accessSecret); } }
From source file:org.n52.twitter.TwitterHarvester.java
License:Open Source License
public TwitterHarvester() { InputStream is = getClass().getResourceAsStream(TWITTER_CREDENTIALS_PROPERTIES); if (is == null) { throw new IllegalStateException(TWITTER_CREDENTIALS_PROPERTIES + " file not found."); }//from w w w . jav a2 s .co m Properties props = new Properties(); try { props.load(is); String accessToken = props.getProperty("ACCESS_TOKEN"); String accessTokenSecret = props.getProperty("ACCESS_TOKEN_SECRET"); this.accessToken = new AccessToken(accessToken, accessTokenSecret); this.oauthConsumerSecret = props.getProperty("OAUTH_CONSUMER_SECRET"); this.oauthConsumerKey = props.getProperty("OAUTH_CONSUMER_KEY"); if (props.containsKey("SEARCH_TERMS") && !(props.getProperty("SEARCH_TERMS") == null) && !props.getProperty("SEARCH_TERMS").isEmpty()) { this.searchTerms = Arrays.asList(props.getProperty("SEARCH_TERMS").split(",")); } } catch (IOException e) { LOGGER.warn("properties malformed or unreadable", e); throw new IllegalStateException(e); } }
From source file:org.nsoft.openbus.action.SendTwitteAction.java
License:Open Source License
@Override public void execute(Activity a, String message, Bundle b) { TwitterAccount twitterAcc = Account.getTwitterAccount(a); long idReplyStatus = -1; if (b != null) idReplyStatus = b.getLong("inReply"); if (idReplyStatus != -1) { inReplyId = "" + inReplyId; StatusUpdate update = new StatusUpdate(message); update.setInReplyToStatusId(idReplyStatus); try {/* ww w .j av a 2s . c o m*/ TwitterUtils.getTwitter(new AccessToken(twitterAcc.getToken(), twitterAcc.getTokenSecret())) .updateStatus(update); } catch (TwitterException e) { } } else twitterAcc.updateStatus(message); messageSent = true; }
From source file:org.nsoft.openbus.model.TwitterAccount.java
License:Open Source License
@Override public URL processProfileImage() { Twitter twitter = TwitterUtils.getTwitter(new AccessToken(token, tokenSecret)); try {/*from ww w . j a v a 2 s . c o m*/ return new URL(twitter.verifyCredentials().getOriginalProfileImageURL()); } catch (TwitterException e) { return null; } catch (MalformedURLException e) { return null; } }
From source file:org.nsoft.openbus.model.TwitterAccount.java
License:Open Source License
@Override public boolean updateStatus(String status) { try {//from ww w . java 2s . c o m TwitterUtils.getTwitter(new AccessToken(token, tokenSecret)).updateStatus(status); return true; } catch (TwitterException e) { return false; } }
From source file:org.nsoft.openbus.view.SearchColumn.java
License:Open Source License
@Override protected void updateList() { TwitterAccount user = Account.getTwitterAccount(ctx); if (user == null) return;/* www.j a v a2 s .c o m*/ AccessToken token = new AccessToken(user.getToken(), user.getTokenSecret()); new GetSearchTweetsTask(token).execute(); JSONObject prop = config.getProprietes(); if (prop != null) { prop.remove("nextPage"); try { prop.put("nextPage", currentpage); } catch (JSONException e) { } } }
From source file:org.opencrx.application.twitter.SendDirectMessageWorkflow.java
License:BSD License
@Override public void execute(WorkflowTarget wfTarget, ContextCapable targetObject, WfProcessInstance wfProcessInstance) throws ServiceException { PersistenceManager pm = JDOHelper.getPersistenceManager(wfProcessInstance); PersistenceManager rootPm = pm.getPersistenceManagerFactory() .getPersistenceManager(SecurityKeys.ROOT_PRINCIPAL, null); try {/*from ww w . j a va 2 s .com*/ UserHome userHome = (UserHome) pm.getObjectById(wfProcessInstance.refGetPath().getParent().getParent()); String providerName = userHome.refGetPath().get(2); String segmentName = userHome.refGetPath().get(4); ComponentConfiguration configuration = TwitterUtils.getComponentConfiguration(providerName, segmentName, rootPm); Map<String, Object> params = WorkflowHelper.getWorkflowParameters(wfProcessInstance); // Find default twitter account TwitterAccountQuery twitterAccountQuery = (TwitterAccountQuery) pm.newQuery(TwitterAccount.class); twitterAccountQuery.thereExistsIsDefault().isTrue(); twitterAccountQuery.thereExistsIsActive().isTrue(); List<TwitterAccount> twitterAccounts = userHome.getEMailAccount(twitterAccountQuery); TwitterAccount twitterAccount = twitterAccounts.isEmpty() ? null : twitterAccounts.iterator().next(); String subject = null; String text = null; if (twitterAccount == null) { subject = "ERROR: " + Notifications.getInstance().getNotificationSubject(pm, targetObject, wfProcessInstance.refGetPath(), userHome, params, true // useSendMailSubjectPrefix ); text = "ERROR: direct message not sent. No default twitter account\n"; } else { // Send direct message subject = Notifications.getInstance().getNotificationSubject(pm, targetObject, wfProcessInstance.refGetPath(), userHome, params, true //this.useSendMailSubjectPrefix ); String tinyUrl = TinyUrlUtils .getTinyUrl(Notifications.getInstance().getAccessUrl(targetObject.refGetPath(), userHome)); if (tinyUrl != null) { if (subject.length() > TwitterUtils.MESSAGE_SIZE - 30 - 4) { subject = subject.substring(0, TwitterUtils.MESSAGE_SIZE - 30 - 4) + "..."; } subject += " " + tinyUrl; } else { if (subject.length() > TwitterUtils.MESSAGE_SIZE - 3) { subject = subject.substring(0, TwitterUtils.MESSAGE_SIZE - 3) + "..."; } } TwitterFactory twitterFactory = new TwitterFactory( ); AccessToken accessToken = new AccessToken(twitterAccount.getAccessToken(), twitterAccount.getAccessTokenSecret()); Twitter twitter = twitterFactory.getInstance(); twitter.setOAuthConsumer(TwitterUtils.getConsumerKey(twitterAccount, configuration), TwitterUtils.getConsumerSecret(twitterAccount, configuration)); twitter.setOAuthAccessToken(accessToken); SysLog.detail("Send direct message message"); DirectMessage message = twitter.sendDirectMessage(twitterAccount.getName(), subject); SysLog.detail("Done", message); } WorkflowHelper.createLogEntry(wfProcessInstance, subject, text); } catch (Exception e) { SysLog.warning("Can not send direct message"); ServiceException e0 = new ServiceException(e); SysLog.detail(e0.getMessage(), e0.getCause()); WorkflowHelper.createLogEntry(wfProcessInstance, "Can not send direct message: Exception", e.getMessage()); throw e0; } finally { if (rootPm != null) { rootPm.close(); } } }
From source file:org.opencrx.kernel.portal.wizard.TwitterSendMessageWizardController.java
License:BSD License
/** * OK action./*from w w w . ja v a2s . c om*/ * * @param formFields * @throws ServiceException */ public void doOK(@FormParameter(forms = "TwitterSendMessageForm") Map<String, Object> formFields) throws ServiceException { PersistenceManager pm = this.getPm(); RefObject_1_0 obj = this.getObject(); this.doRefresh(formFields); String toUsers = (String) formFields.get("org:opencrx:kernel:base:SendAlertParams:toUsers"); String text = (String) formFields.get("org:opencrx:kernel:base:Note:text"); if ((toUsers != null) && !toUsers.isEmpty() && (text != null) && !text.isEmpty()) { javax.jdo.PersistenceManager rootPm = pm.getPersistenceManagerFactory() .getPersistenceManager(org.opencrx.kernel.generic.SecurityKeys.ROOT_PRINCIPAL, null); org.opencrx.kernel.admin1.jmi1.ComponentConfiguration configuration = org.opencrx.application.twitter.TwitterUtils .getComponentConfiguration(this.getProviderName(), this.getSegmentName(), rootPm); // Find default twitter account org.opencrx.kernel.home1.jmi1.UserHome userHome = UserHomes.getInstance().getUserHome(obj.refGetPath(), pm); TwitterAccountQuery twitterAccountQuery = (TwitterAccountQuery) pm.newQuery(TwitterAccount.class); twitterAccountQuery.thereExistsIsDefault().isTrue(); twitterAccountQuery.thereExistsIsActive().isTrue(); List<TwitterAccount> twitterAccounts = userHome.getEMailAccount(twitterAccountQuery); TwitterAccount twitterAccount = twitterAccounts.isEmpty() ? null : twitterAccounts.iterator().next(); if (twitterAccount != null) { TwitterFactory twitterFactory = new TwitterFactory(); AccessToken accessToken = new AccessToken(twitterAccount.getAccessToken(), twitterAccount.getAccessTokenSecret()); Twitter twitter = twitterFactory.getInstance(); twitter.setOAuthConsumer(TwitterUtils.getConsumerKey(twitterAccount, configuration), TwitterUtils.getConsumerSecret(twitterAccount, configuration)); twitter.setOAuthAccessToken(accessToken); StringTokenizer users = new StringTokenizer(toUsers, " ,", false); while (users.hasMoreTokens()) { String user = users.nextToken(); try { @SuppressWarnings("unused") DirectMessage message = twitter.sendDirectMessage(user, text); } catch (Exception e) { new ServiceException(e).log(); } } // Send alert if invoked on user's home String title = NOTE_TITLE_PREFIX + " [" + twitterAccount.getName() + "] @ " + new Date(); if (obj instanceof org.opencrx.kernel.home1.jmi1.UserHome) { org.opencrx.kernel.home1.jmi1.UserHome user = (org.opencrx.kernel.home1.jmi1.UserHome) obj; Base.getInstance().sendAlert(user, // target user.refGetPath().getBase(), title, text, (short) 2, // importance 0, // resendDelayInSeconds null // reference ); } // Attach message as Note else if (obj instanceof org.opencrx.kernel.generic.jmi1.CrxObject) { try { pm.currentTransaction().begin(); org.opencrx.kernel.generic.jmi1.CrxObject crxObject = (org.opencrx.kernel.generic.jmi1.CrxObject) obj; org.opencrx.kernel.generic.jmi1.Note note = pm .newInstance(org.opencrx.kernel.generic.jmi1.Note.class); note.setTitle(title); note.setText(text); crxObject.addNote(Base.getInstance().getUidAsString(), note); pm.currentTransaction().commit(); // FollowUp if obj is an activity if (obj instanceof Activity) { try { Activity activity = (Activity) obj; ActivityProcess activityProcess = activity.getActivityType().getControlledBy(); ActivityProcessTransitionQuery processTransitionQuery = (ActivityProcessTransitionQuery) pm .newQuery( org.opencrx.kernel.activity1.cci2.ActivityProcessTransition.class); processTransitionQuery.thereExistsPrevState().equalTo(activity.getProcessState()); processTransitionQuery.orderByNewPercentComplete().ascending(); List<ActivityProcessTransition> processTransitions = activityProcess .getTransition(processTransitionQuery); if (!processTransitions.isEmpty()) { ActivityProcessTransition processTransition = processTransitions.iterator() .next(); pm.currentTransaction().begin(); @SuppressWarnings("unused") ActivityFollowUp followUp = Activities.getInstance().doFollowUp(activity, title, text, processTransition, null, // reportingContact null // parentProcessInstance ); pm.currentTransaction().commit(); } } catch (Exception e) { try { pm.currentTransaction().rollback(); } catch (Exception e0) { } } } } catch (Exception e) { try { pm.currentTransaction().rollback(); } catch (Exception e0) { } } } this.forward("Cancel", this.getRequest().getParameterMap()); } } }
From source file:org.opencrx.kernel.portal.wizard.TwitterUpdateStatusWizardController.java
License:BSD License
/** * OK action./*from w w w . ja v a 2s . com*/ * * @param formFields * @param serviceAccountId * @throws ServiceException */ public void doOK(@FormParameter(forms = "TwitterUpdateStatusForm") Map<String, Object> formFields, @RequestParameter(name = "serviceAccountId") String serviceAccountId) throws ServiceException { PersistenceManager pm = this.getPm(); this.doRefresh(formFields, serviceAccountId); RefObject_1_0 obj = this.getObject(); javax.jdo.PersistenceManager rootPm = pm.getPersistenceManagerFactory() .getPersistenceManager(org.opencrx.kernel.generic.SecurityKeys.ROOT_PRINCIPAL, null); org.opencrx.kernel.home1.jmi1.UserHome userHome = UserHomes.getInstance().getUserHome(obj.refGetPath(), pm); serviceAccountId = (String) formFields.get("serviceAccountId"); String text = (String) formFields.get("org:opencrx:kernel:base:Note:text"); if ((serviceAccountId != null) && (serviceAccountId.length() > 0) && (text != null) && (text.length() > 0)) { org.opencrx.kernel.admin1.jmi1.ComponentConfiguration configuration = org.opencrx.application.twitter.TwitterUtils .getComponentConfiguration(this.getProviderName(), this.getSegmentName(), rootPm); // Find default twitter account org.opencrx.kernel.home1.jmi1.TwitterAccount twitterAccount = (org.opencrx.kernel.home1.jmi1.TwitterAccount) userHome .getEMailAccount(serviceAccountId); if (twitterAccount != null) { TwitterFactory twitterFactory = new TwitterFactory(); AccessToken accessToken = new AccessToken(twitterAccount.getAccessToken(), twitterAccount.getAccessTokenSecret()); Twitter twitter = twitterFactory.getInstance(); twitter.setOAuthConsumer(TwitterUtils.getConsumerKey(twitterAccount, configuration), TwitterUtils.getConsumerSecret(twitterAccount, configuration)); twitter.setOAuthAccessToken(accessToken); try { @SuppressWarnings("unused") Status status = twitter.updateStatus(text); } catch (Exception e) { new ServiceException(e).log(); } // Send alert if invoked on user's home String title = NOTE_TITLE_PREFIX + " [" + twitterAccount.getName() + "] @ " + new Date(); if (obj instanceof org.opencrx.kernel.home1.jmi1.UserHome) { org.opencrx.kernel.home1.jmi1.UserHome user = (org.opencrx.kernel.home1.jmi1.UserHome) obj; Base.getInstance().sendAlert(user, // target user.refGetPath().getBase(), title, text, (short) 2, // importance 0, // resendDelayInSeconds null // reference ); } // Attach message as Note else if (obj instanceof org.opencrx.kernel.generic.jmi1.CrxObject) { try { pm.currentTransaction().begin(); org.opencrx.kernel.generic.jmi1.CrxObject crxObject = (org.opencrx.kernel.generic.jmi1.CrxObject) obj; org.opencrx.kernel.generic.jmi1.Note note = pm .newInstance(org.opencrx.kernel.generic.jmi1.Note.class); note.setTitle(title); note.setText(text); crxObject.addNote(Base.getInstance().getUidAsString(), note); pm.currentTransaction().commit(); // FollowUp if obj is an activity if (obj instanceof Activity) { try { Activity activity = (Activity) obj; ActivityProcess activityProcess = activity.getActivityType().getControlledBy(); org.opencrx.kernel.activity1.cci2.ActivityProcessTransitionQuery processTransitionQuery = (org.opencrx.kernel.activity1.cci2.ActivityProcessTransitionQuery) pm .newQuery( org.opencrx.kernel.activity1.cci2.ActivityProcessTransition.class); processTransitionQuery.thereExistsPrevState().equalTo(activity.getProcessState()); processTransitionQuery.orderByNewPercentComplete().ascending(); List<ActivityProcessTransition> processTransitions = activityProcess .getTransition(processTransitionQuery); if (!processTransitions.isEmpty()) { ActivityProcessTransition processTransition = processTransitions.iterator() .next(); pm.currentTransaction().begin(); @SuppressWarnings("unused") ActivityFollowUp followUp = Activities.getInstance().doFollowUp(activity, title, text, processTransition, null, // reportingContact null // parentProcessInstance ); pm.currentTransaction().commit(); } } catch (Exception e) { try { pm.currentTransaction().rollback(); } catch (Exception e0) { } } } } catch (Exception e) { try { pm.currentTransaction().rollback(); } catch (Exception e0) { } } } this.forward("Cancel", this.getRequest().getParameterMap()); } } }