そらいろさんぽ

個人で開発したアプリやウェブサイトの情報をまとめたブログです

tmhOAuthを使ったTwitter認証について

[PHP]tmhOAuthを使ったTwitter認証について

f:id:soranokurousagi:20150824220700p:plain
こんばんは。そらのくろうさぎです。
公式のTwitter開発者用サイトで紹介されている「tmhOAuth」の使い方を備忘録としてまとめます。

tmhOAuthについて

GitHub:https://github.com/themattharris/tmhOAuth

アプリ登録

Twitter Application Management:https://apps.twitter.com

Twitterアプリ管理サイトでアプリを登録し、Consumer KeyとConsumer Secretをコピーします。

ソース

<?php
include_once 'library/tmhOAuth/tmhOAuth.php';

session_start();
$callback = 'http://xxxxxxxx.com/twitter_oauth.php';
$consumers = array(
    'consumer_key' => 'Consumer Key',
    'consumer_secret' => 'Consumer Secret',
);

if (!empty($_SESSION['access_token'])) {
    $tmhOAuth = new tmhOAuth(array(
        'user_token' => $_SESSION['access_token']['oauth_token'],
        'user_secret' => $_SESSION['access_token']['oauth_token_secret'],
    ) + $consumers);
    $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline', 'json'));
    if ($code == 200) {
        $responses = json_decode($tmhOAuth->response['response'], true);
        print_r($responses);
        exit;
    }
} else if (!empty($_SESSION['request_token'])) {
    $tmhOAuth = new tmhOAuth(array(
        'user_token' => $_SESSION['request_token']['oauth_token'],
        'user_secret' => $_SESSION['request_token']['oauth_token_secret'],
    ) + $consumers);
    $tmhOAuth->config['user_token'] = $_SESSION['request_token']['oauth_token'];
    $tmhOAuth->config['user_secret'] = $_SESSION['request_token']['oauth_token_secret'];
    unset($_SESSION['request_token']);

    $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', false), array(
        'oauth_verifier' => $_GET['oauth_verifier']
    ));
    if ($code == 200) {
        $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response["response"]);
        header('Location: '.$callback);
        exit ;
    }
} else {
    $tmhOAuth = new tmhOAuth($consumers);
    $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', false), array(
        'oauth_callback' => $callback
    ));
    if ($code == 200) {
        $requests = $tmhOAuth->extract_params($tmhOAuth->response["response"]);
        $_SESSION['request_token'] = $requests;
        $authUrl = $tmhOAuth->url("oauth/authorize", false)."?oauth_token=".$requests['oauth_token'];
        header('Location: '.$authUrl);
        exit ;
    }
}
die('Twitterの認証に失敗しました');
?>

解説

認証のためのセッションを開始します

session_start();

Twitter認証後のコールバックURLを記述します。Twitterのアプリ認証が終わるとこちらのURLにリダイレクトされます。

$callback = 'http://xxxxxxxx.com/twitter_oauth.php';

$consumers変数にアプリ登録でコピーしたConsumer KeyとConsumer Secretをコピーします。

$consumers = array(
    'consumer_key' => 'Consumer Key',
    'consumer_secret' => 'Consumer Secret',
);

Twitter認証後に取得したアクセストークンをtmhOAuthに設定します。tmhOAuth::request()からはHTTPステータスコードが返されます。

$tmhOAuth = new tmhOAuth(array(
    'user_token' => $_SESSION['access_token']['oauth_token'],
    'user_secret' => $_SESSION['access_token']['oauth_token_secret'],
) + $consumers);
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline', 'json'));

Twitter認証前に取得したリクエストトークンをtmhOAuthに設定します。

$tmhOAuth = new tmhOAuth(array(
    'user_token' => $_SESSION['request_token']['oauth_token'],
    'user_secret' => $_SESSION['request_token']['oauth_token_secret'],
) + $consumers);
$tmhOAuth->config['user_token'] = $_SESSION['request_token']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['request_token']['oauth_token_secret'];

アクセストークンをセッションに保存します。

$_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response["response"]);

リクエストトークンを取得します。tmhOAuth::request()からはHTTPステータスコードが返されます。

$tmhOAuth = new tmhOAuth($consumers);
$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', false), array(
    'oauth_callback' => $callback
));

セッションにリクエストトークンを保存します。

$requests = $tmhOAuth->extract_params($tmhOAuth->response["response"]);
$_SESSION['request_token'] = $requests;

Twitter認証用のURLを生成します。

$authUrl = $tmhOAuth->url("oauth/authorize", false)."?oauth_token=".$requests['oauth_token'];

以上、tmhOAuthを使ったTwitter認証でした。ではでは〜。