Skip to content

Instantly share code, notes, and snippets.

@roundrop
Forked from kimukou/env.txt
Created September 28, 2012 15:24
Show Gist options
  • Save roundrop/3800508 to your computer and use it in GitHub Desktop.
Save roundrop/3800508 to your computer and use it in GitHub Desktop.
facebook4j_twitter4j_compare
GroovyConsleの動きがいまいちの場合は以下を修正すること
(groovyConsoleのシェルの方を治すのも可。特にMac!!)
$GROOVY_HOME/bin
startGroovy
## 先頭に
export JAVA_HOME=/Library/Java/Home
export JAVA_OPTS='-Dgroovy.source.encoding=UTF-8 -Dfile.encoding=UTF-8'
//=====================================================
//App Key
oAuthAppId="XXXXXX"
oAuthAppSecret="XXXXXX"
oAuthPermissions="email,publish_stream,read_stream"
//OAuth token
//ランタイムトークンは 以下から取得すること(有効期限は短いらしい。上記の権限は手動で付ける必要はあり)
// https://developers.facebook.com/tools/explorer/
token = ""
//=====================================================
@Grab('org.facebook4j:facebook4j-core:[1.0,)')
import facebook4j.*
import facebook4j.api.*
import facebook4j.auth.AccessToken
import facebook4j.conf.ConfigurationBuilder
accessToken = new AccessToken(token)
confbuilder = new ConfigurationBuilder()
confbuilder
.setOAuthAccessToken(accessToken.getToken())
.setOAuthAppId(oAuthAppId)
.setOAuthAppSecret(oAuthAppSecret)
.setOAuthPermissions(oAuthPermissions)
facebook = new FacebookFactory(confbuilder.build()).getInstance()
//println facebook.dump()
//see http://facebook4j.org/en/javadoc/facebook4j/api/PostMethods.html
//投稿
facebook.postStatusMessage("てすと")
//AT の場合は以下
//facebook.postStatusMessage(userid,"てすと")
//ユーザホーム取得
facebook.getHome().each{tweet->
//println it.dump()
println "[${tweet.class.name}]${tweet.from.name}:${tweet.message}"
}
println "---------------------------------------------------------"
//see http://facebook4j.org/en/javadoc/facebook4j/api/SearchMethods.html#search(java.lang.String)
//検索
result = facebook.searchPosts("twitter4j")
result.each{tweet->
//println tweet.dump()
println "[${tweet.class.name}]${tweet.from.name}:${tweet.message}"
}
[facebook4j.internal.json.PostJSONImpl]null:豚カツ発祥の地らしいうちの会社の周辺には豚カツ屋がゴロゴロあるけど、よく考えるとカツカレー出してる店って殆ど無いって事に気付いた。
[facebook4j.internal.json.PostJSONImpl]「もっと自由に働きたい」常識を疑え、今すぐつくり出せ!そこからだ:null
[facebook4j.internal.json.PostJSONImpl]「もっと自由に働きたい」常識を疑え、今すぐつくり出せ!そこからだ:null
[facebook4j.internal.json.PostJSONImpl]null:朝飯
[facebook4j.internal.json.PostJSONImpl]デザイン・ドリブン・イノベーション:null
---------------------------------------------------------
[facebook4j.internal.json.PostJSONImpl]null:دلوقتي لو انا عايز اظبط حاجه من twitter4j اعمل ايه خصوصا اني حاطتها في اreference libraries بس برده مش عايز يعملها import
??????????
//========================================================
//App Key
consumerKey="XXXXX"
consumerSecret="XXXXX"
//OAuth token
token = "XXXXXX"
tokenSecret = "XXXXX"
//========================================================
@Grab('org.twitter4j:twitter4j-core:2.2.6')
import twitter4j.*
import twitter4j.auth.AccessToken
import twitter4j.conf.ConfigurationBuilder
accessToken = new AccessToken(token,tokenSecret)
confbuilder = new ConfigurationBuilder()
confbuilder
.setOAuthAccessToken(accessToken.getToken())
.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
twitter = new TwitterFactory(confbuilder.build()).getInstance()
//投稿
status = twitter.updateStatus("ふがふが")
println status.dump()
//タイムライン取得
twitter.getHomeTimeline().each{
println it.text
//println it.dump()
}
//検索
query = new Query("source:twitter4j yusukey")
result = twitter.search(query)
result.getTweets().each{tweet->
println "${tweet.getFromUser()}:${tweet.getText()}"
}
@roundrop
Copy link
Author

Facebookの場合、投稿者名はPost->from->nameです。
Post->nameはリンクタイトルとなります。
https://developers.facebook.com/docs/reference/api/post/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment