自作のツイッターアプリを自分のアカウントで認証しようとしていますがうまくいきません。
common.php
php
1 2<?php 3define('CONSUMER_KEY', 'xxxxxxxxxxx'); 4define('CONSUMER_SECRET', 'xxxxxxxxxxx'); 5define('OAUTH_CALLBACK', '/twitter/callback.php');
login.php
php
1<?php 2session_start(); 3 4require_once "common.php"; 5require_once "/twitteroauth/autoload.php"; 6 7use Abraham\TwitterOAuth\TwitterOAuth; 8 9$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); 10 11 12$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); 13 14$_SESSION['oauth_token'] = $request_token['oauth_token']; 15$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 16 17$url = $connection->url('oauth/authenticate', array('oauth_token' => $request_token['oauth_token'])); 18 19header("location: ".$url);
callback.php
php
1<?php 2session_start(); 3 4require_once "common.php"; 5require_once "/twitteroauth/autoload.php"; 6 7use Abraham\TwitterOAuth\TwitterOAuth; 8 9$request_token = []; 10 11var_dump($_SESSION); 12 13$request_token['oauth_token'] = $_SESSION['oauth_token']; 14$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; 15 16if (isset($_REQUEST['oauthtoken']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) { 17 die("Error!"); 18} 19 20$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']); 21 22header("location", "mypage.php");
mypage.php
php
1<?php 2session_start(); 3 4require_once "common.php"; 5require_once '/twitteroauth/autoload.php'; 6 7use Abraham\TwitterOAuth\TwitterOAuth; 8 9$access_token = $_SESSION['access_token']; 10 11$connection = new Twitter(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_seret']); 12 13$user = $connection->get("account/verify_credntials"); 14 15var_dump($user); 16 17echo $access_token['oauth_token']."\n".$access_token['oauth_token_seret'];
認証ボタンを押した後のcallback.phpで
Notice: Undefined index: oauth_token in callback.php on line 14 Notice: Undefined index: oauth_token_secret in callback.php on line 15
というエラーが出てきます。
var_dump($_SESSION)は
array(0) { }
で、$_SESSIONが保存されていないようです。
セッションが保存されない原因は何でしょうか?
環境は
PHP7
Google Chrome
で、ローカルで動かしています。
追記1
login.phpでvar_dump($request_token)したところ
array(3) { ["oauth_token"]=> string(27) "Eb8QEgAAAAAA6V55AAABZk08S6E" ["oauth_token_secret"]=> string(32) "FcJGrcDCGGImPrxvlPztnoBC4gJEGCiD" ["oauth_callback_confirmed"]=> string(4) "true" }
と出ました。
callback.phpでvar_dump($_COOKIE["PHPSESSID"]);で出た文字列と、xampp/tmpに入っている最新のセッションファイルのsess_以降の文字列は違うものでした。
追記2
session_id()を使ってlogin.phpとcallback.phpでのセッションIDを比較しました。
login.phpではma7b486qh9tt1rbpejfs7i45ul
callback.phpではa7if3osv2vc17qbgs8eupk4rf8
でした。
あなたの回答
tips
プレビュー