実は10日の話になるんですがtwitter認証の話をやろうと思います。
Twitterの認証を実装しましてね。まぁやってることは単純なんですけど、とりあえずサクっと紹介したいと思います。
まず使用するライブラリとしては
thujohn / twitterってのがGitHubに落ちてたのでこちらを採用しました。テキトーに
Twitter Laravelって調べると出てきて、わりかし使いやすそうであったので・・・・・・。
別にSocialiteでいいじゃんって感じもしなくもないんですけどね(笑)
ライブラリの入れ方はめっちゃ簡単ですね。マニュアルに書いてある通りです。
動かし方もExampleとほぼ同じコードを書いていれば動くんですよね・・・・・・。
まぁ注意点としては、twitterの方にcallbackURLをちゃんと指定していないと動かないってとこなんですかね。Facebookとかでもセキュリティ対策でそーいうことされてるみたいですけど。
まぁ、こんな感じにですね。
で、ユーザー情報を取得すると、以下みたいなのが取れます。
object(stdClass)#280 (45) {
["id"]=>
int(156581751)
["id_str"]=>
string(9) "156581751"
["name"]=>
string(9) "ろんぐ"
["screen_name"]=>
string(7) "trc_vla"
["location"]=>
string(18) "兵庫→神奈川"
["profile_location"]=>
NULL
["description"]=>
string(262) "酒とコードとオーケストラ(笑) ちなみに横国オケ出身です。フォローは自由にお気軽に。笑/ テネフィル,メモリアル'17&'18/ 仕事やめて起業することにしました。ニートにもどっちゃったね。(笑)"
["url"]=>
string(23) "https://t.co/HN8nWJAmk6"
["entities"]=>
object(stdClass)#283 (2) {
["url"]=>
object(stdClass)#282 (1) {
["urls"]=>
array(1) {
[0]=>
object(stdClass)#281 (4) {
["url"]=>
string(23) "https://t.co/HN8nWJAmk6"
["expanded_url"]=>
string(28) "http://trc-vlackhistory.net/"
["display_url"]=>
string(20) "trc-vlackhistory.net"
["indices"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(23)
}
}
}
}
["description"]=>
object(stdClass)#284 (1) {
["urls"]=>
array(0) {
}
}
}
["protected"]=>
bool(false)
["followers_count"]=>
int(472)
["friends_count"]=>
int(459)
["listed_count"]=>
int(3)
["created_at"]=>
string(30) "Thu Jun 17 09:23:55 +0000 2010"
["favourites_count"]=>
int(170)
["utc_offset"]=>
int(32400)
["time_zone"]=>
string(10) "Asia/Tokyo"
["geo_enabled"]=>
bool(false)
["verified"]=>
bool(false)
["statuses_count"]=>
int(96836)
["lang"]=>
string(2) "ja"
["status"]=>
object(stdClass)#285 (22) {
["created_at"]=>
string(30) "Tue Oct 10 06:44:57 +0000 2017"
["id"]=>
int(917642089378201601)
["id_str"]=>
string(18) "917642089378201601"
["text"]=>
string(78) "でも聞き終わった後一人で作業したいしなぁとか思うとネ"
["truncated"]=>
bool(false)
["entities"]=>
object(stdClass)#286 (4) {
["hashtags"]=>
array(0) {
}
["symbols"]=>
array(0) {
}
["user_mentions"]=>
array(0) {
}
["urls"]=>
array(0) {
}
}
["source"]=>
string(66) "Twitter Web Client"
["in_reply_to_status_id"]=>
NULL
["in_reply_to_status_id_str"]=>
NULL
["in_reply_to_user_id"]=>
NULL
["in_reply_to_user_id_str"]=>
NULL
["in_reply_to_screen_name"]=>
NULL
["geo"]=>
NULL
["coordinates"]=>
NULL
["place"]=>
NULL
["contributors"]=>
NULL
["is_quote_status"]=>
bool(false)
["retweet_count"]=>
int(0)
["favorite_count"]=>
int(0)
["favorited"]=>
bool(false)
["retweeted"]=>
bool(false)
["lang"]=>
string(2) "ja"
}
["contributors_enabled"]=>
bool(false)
["is_translator"]=>
bool(false)
["is_translation_enabled"]=>
bool(false)
["profile_background_color"]=>
string(6) "FFFFFF"
["profile_background_image_url"]=>
string(48) "http://abs.twimg.com/images/themes/theme4/bg.gif"
["profile_background_image_url_https"]=>
string(49) "https://abs.twimg.com/images/themes/theme4/bg.gif"
["profile_background_tile"]=>
bool(false)
["profile_image_url"]=>
string(74) "http://pbs.twimg.com/profile_images/451355516527054848/L0cJmGab_normal.png"
["profile_image_url_https"]=>
string(75) "https://pbs.twimg.com/profile_images/451355516527054848/L0cJmGab_normal.png"
["profile_link_color"]=>
string(6) "1B95E0"
["profile_sidebar_border_color"]=>
string(6) "CCDDEE"
["profile_sidebar_fill_color"]=>
string(6) "DDEEFF"
["profile_text_color"]=>
string(6) "3366FF"
["profile_use_background_image"]=>
bool(false)
["has_extended_profile"]=>
bool(true)
["default_profile"]=>
bool(false)
["default_profile_image"]=>
bool(false)
["following"]=>
bool(false)
["follow_request_sent"]=>
bool(false)
["notifications"]=>
bool(false)
["translator_type"]=>
string(4) "none"
["suspended"]=>
bool(false)
["needs_phone_verification"]=>
bool(false)
}
割といろんな情報がとれますね。こっから欲しい情報を抜き取って使う感じですね。
Livvonではツイートには触れないつもりで、ただIDとニックネームを引っこ抜いてくる感じになりますかね。apiから取れたものしか受け付けないロジックです。
ただ、トークンの取得が100万件に制限されているってのが非常に気になるんですよね・・・・・・。なんかそのうち問題が出てきそうではあるんですが、まぁ気にしないことにします。(何)
とりあえず、思ったより簡単にTwitter認証はできてしまいました。Facebookも実装できましたし、こんなもんかな・・・・・・。