2016-06-29 2 views
0

방금 ​​내 트위터 앱에 승인을 준 사용자의 access_token을 얻으려고합니다. 로그인 할 때 작동하지만, 새로 고침하거나 버튼을 클릭하기 전까지는 내 access_token 만 기억합니다. 내가 사용아브라함의 TwitterOAuth access_token은 새로 고침 또는 버튼 클릭 후 사라집니다.

코드 :

require(__DIR__ . '/../../lib/data/twitter-login-api/autoload.php'); 
use Abraham\TwitterOAuth\TwitterOAuth; 

$oauth_callback = OAUTH_CALLBACK; 
$consumer_key = OAUTH_KEY; 
$consumer_secret = OAUTH_SECRET; 

//Do something if $_REQUEST occur and previous session is equal to current $_REQUEST 
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] === $_REQUEST['oauth_token']) { 

    //Open first connection at callback 
    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

    //Then verify your token 
    $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 

    //Now you can save them 
    $_SESSION['new_session_for_oauth_token'] = $access_token['oauth_token']; 
    $_SESSION['new_session_for_oauth_token_secret'] = $access_token['oauth_token_secret']; 

    //You may also check it first 
    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['new_session_for_oauth_token'], $_SESSION['new_session_for_oauth_token_secret']); 
    $check = $connection->get("account/verify_credentials"); 
    $username = $check->name; 

    //To echo your account's stat 
    echo '<p>' . $check->statuses_count . '</p>'; 
    echo '<p>' . $check->friends_count . '</p>'; 
    echo '<p>' . $check->followers_count . '</p>'; 
    echo '<p>' . $check->favourites_count . '</p>'; 

    //And finally unset previous sessions 
    unset($_SESSION['oauth_token']); 
    unset($_SESSION['oauth_token_secret']); 

    //this is the end of callback url 
} else { 

    $connection = new TwitterOAuth($consumer_key, $consumer_secret); 
    $request_token = $connection->oauth('oauth/request_token', array("oauth_callback" => $oauth_callback)); 

    $_SESSION['oauth_token'] = $request_token['oauth_token'];  
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

    $twitter_url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token'])); 
} 

그것은 한 번에 나를 기록하고 나는 access_token이와 access_token_secret를받을 그러나 나는 또한 페이지 후에 사용할 수 있도록 내가 세션에 머물 그들을 필요 새로 고침하거나 버튼을 클릭하십시오.

내가 뭘 잘못하고 있니?

답변

0

액세스 토큰을 확인하기 전에 "액세스가 불가능한"연결을 인스턴스화했기 때문에 발생한다고 생각했습니다. 맨 처음에서

,
//Open first connection at callback 
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

//Then verify your token 
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 

//Now you can save them 
$_SESSION['new_session_for_oauth_token'] = $access_token['oauth_token']; 
$_SESSION['new_session_for_oauth_token_secret'] = $access_token['oauth_token_secret']; 

당신이 그 통지해야하는 또 다른 것은, 당신이 필요합니까

unset 이전 (이전에 승인) 토큰하지만 물론 권한을 부여하는 단계가 지난 후이다. 그리고 여기, 당신이 "요청"에 대해 하나의 페이지가 당신이 트위터에 로그인려고하는 경우에, 당신의 HTML 부분에,

//Do something if $_REQUEST occur and previous session is equal to current $_REQUEST 
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] === $_REQUEST['oauth_token']) { 

    //Open first connection at callback 
    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

    //Then verify your token 
    $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 

    //Now we overwrite previouse session with verified one 
    $_SESSION['oauth_token'] = $access_token['oauth_token']; 
    $_SESSION['oauth_token_secret'] = $access_token['oauth_token_secret']; 

    //You may also check it first 
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['new_session_for_oauth_token'], $_SESSION['new_session_for_oauth_token_secret']); 
    $check = $connection->get("account/verify_credentials"); 

    //To echo your account's stat 
    echo $check->statuses_count; 
    echo $check->friends_count; 
    echo $check->followers_count; 
    echo $check->favourites_count; 

    //And finally unset previous sessions 
    unset($_SESSION['oauth_token']); 
    unset($_SESSION['oauth_token_secret']); 

    //this is the end of callback url 
} elseif (isset($_GET['twitter']) && $_GET['twitter'] === 'login') { 
    //Request a token aka login 

    $connection = new TwitterOAuth($consumer_key, $consumer_secret); 
    $request_token = $connection->oauth('oauth/request_token', array("oauth_callback" => $oauth_callback)); 

    $_SESSION['oauth_token'] = $request_token['oauth_token'];  
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

    $twitter_url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token'])); 
} elseif (isset($_GET['twitter']) && $_GET['twitter'] === 'logout') { 
    //Destroy the session aka logout 

    unset($_SESSION['oauth_token']); 
    unset($_SESSION['oauth_token_secret']); 
    $url = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); 
    header("location: {$url}"); 
    exit(); 
} 

//Before or after this snippet is HTML part 

이제 "콜백"이후 (내 시야에) 더 발현 더 추가 example.org/page.php?twitter=login으로 이동하십시오. 로그 아웃 할 수도 있습니다. example.org/page.php?twitter=logout

+0

답장을 보내 주셔서 감사합니다. 내 코드를 내가 준 예제로 바꿨습니다. 나를 완벽하게 기록하지만 페이지를 새로 고칠 때 여전히 access_token을 잃어 버립니다. 그리고 실제로, 로그인과 콜백을 처리하기 위해 하나의 PHP 파일을 사용하고 있습니다. 그게 가능한가? 위의 코드를 지금 사용하고있는 코드로 편집했습니다. 이 일을 제대로 수행 할 수있는 방법이 있어야합니까? – TomHoevenaars

+0

각 파일마다 다른 로직을 분리하는 것이 더 좋지만,이 상태에서는 스타일의 문제 일뿐입니다. 세션을 어떻게 다시 사용 했습니까? 이 콜백/요청 페이지를 새로 고치시겠습니까? 이 페이지의 (귀하의) 목적은 단지 토큰을 요청하고 확인하는 것만 큼 효과가 없습니다. 다른 페이지 가져 오기, 세션 시작, 새 연결 만들기, 알려주세요. – Chay22

+0

내가 맞다면 세션에서 그 access_token을 유지할 수있는 방법이 있어야합니다. 이 트위터 로그인에 사용하는 PHP 파일은 내 HTML이있는 파일에 포함되어 있습니다. 버튼을 클릭하면 내 앱을 승인해야하는 twitter_url로 리디렉션됩니다. 그 후 내 이름이 나타납니다 (로그인했기 때문에). 또한 access_token 및 access_token_secret가 수신됩니다. 이 부분을 수정하면 로그인 한 계정의 대신에 Tweet를 게시하고 싶습니다. – TomHoevenaars

관련 문제