{"id":7979,"date":"2012-09-24T00:24:36","date_gmt":"2012-09-23T18:54:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7979"},"modified":"2015-05-19T17:44:45","modified_gmt":"2015-05-19T12:14:45","slug":"sending-direct-message-to-a-twitter-user-using-twitter4j","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sending-direct-message-to-a-twitter-user-using-twitter4j\/","title":{"rendered":"Sending &#8216;Direct Message&#8217; to a twitter user using Twitter4j"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project, i worked on integrating twitter API with the application. I used <a href=\"http:\/\/twitter4j.org\/en\/index.html\">Twitter4j<\/a> (a java wrapper for twitter API calls), to integrate the application and use the API calls. You can refer this <a href=\"http:\/\/www.tothenew.com\/blog\/integrating-java-application-with-twitter\/\">blog<\/a> to see how we can integrate the grails application with Twitter.<\/p>\n<pre><\/pre>\n<p>For making any API calls we need to have twitter account <strong>access_token<\/strong> and <strong>access_secret<\/strong>, which we obtains after authorizing\/connecting twitter account with the application as mentioned in this <a href=\"http:\/\/www.tothenew.com\/blog\/integrating-java-application-with-twitter\/\">blog<\/a>.<\/p>\n<pre><\/pre>\n<p><strong>Pre-Requirements:-<\/strong><\/p>\n<p>[java]<br \/>\n       String consumerKey = CONSUMER_KEY \/\/ key obtained after registering app.<br \/>\n       String consumerSecret =CONSUMER_SECRET \/\/ secret key obtained from the registered app.<br \/>\n       String twitterToken = USER_TWITTER_TOKEN<br \/>\n       \/\/ access_token received by authentication user&#8217;s twitter account<br \/>\n       String twitterSecret= USER_TWITTER_SECRET<br \/>\n        \/\/ access_secret obtained by authentication user&#8217;s twitter account<br \/>\n       String directMessage=&#8217;Hi, this is just a test message.&#8217;<br \/>\n       String twitterName = \/\/ twitter name of the receiver.<\/p>\n<p>[\/java]<\/p>\n<pre><\/pre>\n<p><strong>Code to send direct message to Twitter User:-<\/strong><\/p>\n<p>[java]<\/p>\n<p>        TwitterFactory factory = new TwitterFactory()<br \/>\n        Twitter twitter = factory.getInstance()<br \/>\n        twitter.setOAuthConsumer(consumerKey, consumerSecret)<br \/>\n        AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)<br \/>\n        twitter.setOAuthAccessToken(accessToken)<br \/>\n        DirectMessage directMessage = twitter.sendDirectMessage(twitterName, directMessage)<\/p>\n<p>[\/java]<\/p>\n<p>So, the above code will send a direct message to the twitter user provided using API.<br \/>\nThis worked for me.<br \/>\nHope it helps.<\/p>\n<pre><\/pre>\n<p>Other Blogs:-<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/mark-tweet-as-favorite-using-twitter4j\/\">Mark Tweet as Favorite using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/reply-to-a-user-tweet-using-twitter4j\/\">Reply to a tweet using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/retweet-a-tweet-using-twitter4j\/\">Retweet a tweet using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/posting-status-update-on-twitter-using-twitter4j\/\">Post status update using Twitter4j<\/a><\/p>\n<pre><\/pre>\n<p>Cheers!!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i worked on integrating twitter API with the application. I used Twitter4j (a java wrapper for twitter API calls), to integrate the application and use the API calls. You can refer this blog to see how we can integrate the grails application with Twitter. For making any API calls [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[700,882,873,1018,1029,1022,1019],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7979"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7979"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7979\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}