1. TOPTOP
  2. Webサービス
  3. Twitter

Twitterアプリ codebird-phpを使ってログインしようとしてもFatal errorが…

|

Twitter_logo_blue

最近、ちょくちょく、twitterアプリ関連の記事をあげております。

TwitterでログインするWebサービス(新しいタブで開く)を作りたいからやっているわけですが、これがなかなか思ったように画面遷移してくれない…。今回はそんな未解決の問題に関する記事です。

問題の主旨

下記の2つのサイトを参考にして、TwitterでログインするするWebサービスを作っています。

しかし、これらにもとづいて、プログラミングコードを記述してログインしようとしても、

OAuth1

“Fatal error: Uncaught exception ‘Exception’ with message ‘Error 77…”が表示されます。

OAuth2

問題の補足

いろいろとググってみると、同じことで悩んで質問を出されている方がいらしゃいました。質問内容は、今回の私のエラー内容と全く同じです。

この質問の回答者の方が、解決法の1つとして、以下のような修正を提案されているので、私も試してみました。しかし冒頭で申し上げた、同じ“Fatal error”が表示されます。

‘oauth_callback’ => ‘http://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]

‘oauth_callback’ => ‘https://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]

ちなみに、下記のページでもcodebird-phpによるユーザ認証の方法が示されていますが、すべてのコードが、書かれているわけではないので、どのように使うのか、分からない状態でもあります。

エラーメッセージの内容

なお、エラーメッセージと、twitterでログインするためのcallback.phpの詳しい内容は、以下の通りです。

Fatal error: Uncaught exception ‘Exception’ with message ‘Error 77 while validating the Twitter API certificate.’
in /var/www/html/tw_connect_php/codebird.php:923 Stack trace: #0 /var/www/html/tw_connect_php/codebird.php(294)
: Codebird\Codebird->_callApi(‘POST’, ‘oauth/request_t…’, ‘oauth/request_t…’, Array, false, false)
#1 /var/www/html/tw_connect_php/callback.php(17): Codebird\Codebird->__call(‘oauth_requestTo…’, Array)
#2 /var/www/html/tw_connect_php/callback.php(17): Codebird\Codebird->oauth_requestToken(Array)
#3 {main} thrown in /var/www/html/tw_connect_php/codebird.php on line 923

Twitterでログインするためのcallback.php

Twitterでログインするためのcallback.phpの内容は、以下のとおりです。

<?php

require_once('config.php');
require_once('codebird.php');

session_start();

require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey('YOURKEY', 'YOURSECRET'); // static, see 'Using multiple Codebird instances'

$cb = \Codebird\Codebird::getInstance();

if (! isset($_SESSION['oauth_token'])) {
    // get the request token
    $reply = $cb->oauth_requestToken(array(
        'oauth_callback' => 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
    ));

    // store the token
    $cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
    $_SESSION['oauth_token'] = $reply->oauth_token;
    $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
    $_SESSION['oauth_verify'] = true;

    // redirect to auth website
    $auth_url = $cb->oauth_authorize();
    header('Location: ' . $auth_url);
    die();

} elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
    // verify the token
    $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    unset($_SESSION['oauth_verify']);

    // get the access token
    $reply = $cb->oauth_accessToken(array(
        'oauth_verifier' => $_GET['oauth_verifier']
    ));

    // store the token (which is different from the request token!)
    $_SESSION['oauth_token'] = $reply->oauth_token;
    $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;

    // send to same URL, without oauth GET parameters
    header('Location: ' . basename(__FILE__));
    die();
}

// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

〔つづきの記事〕

〔参考サイト〕