我是 Twitter API 的新手,我正在尝试在我的应用程序中获取用户时间线数据。
我已正确设置应用程序设置和 config.php 文件,因此用户成功登录并看到“重定向回您的应用程序”消息,但在重定向到http://MyDomainName.com/callback.php?oauth_token=someToken&oauth_verifier =someOtherToken页面不会显示在浏览器中并且不会发生重定向。手动返回应用程序页面,用户未登录,必须再次登录。谁能帮我解决这个问题?
配置文件
/**
* @file
* A single location to store configuration.
*/
define('CONSUMER_KEY', 'ALPHA_NUMERIC_CONSUMER_KEY');
define('CONSUMER_SECRET', 'ALPHA_NUMERIC_SECRET');
define('OAUTH_CALLBACK', "http://infosys.concordia.ca/MyApps/oauthProxy/callback.php");
define('OAUTH_COOKIE', 'my_twitter_app_oauth');
define('OAUTH_COOKIE_DOMAIN', '.concordia.ca'); //Example ".esri.com"
echo OAUTH_CALLBACK;
//REQUIRED - Encrypt your cookies
//http://si0.twimg.com/images/dev/oauth_diagram.png
//Create your own unique ENCRYPTION_KEY via Encrypt.get_RandomKey()
define('ENCRYPTION_KEY','MY_UNIQUE_ENCRYPTION_KEY');
//Create your own unique initialization vector via Encrypt.get_IV()
define('IV','MY_UNIQUE_IV');
define('DEFAULT_TIME_ZONE','America/Toronto');
卡拉布克.php
<?php
//Version 2.1 by AndyG 4/2013
//Changes
//- added OAuth Encrption
// Start session and load lib
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('twitteroauth/Encrypt.php');
require_once('config.php');
$content = null; //for verification of credentials
$connection = null; //for getting access token
// check if cookie exists
if(isset($_COOKIE[OAUTH_COOKIE])){
// redirect back to app
if(isset($_SESSION['oauth_referrer'])){
header('Location: '.$_SESSION['oauth_referrer']);
exit;
}
}
else{
// if verifier set
if(isset($_REQUEST['oauth_verifier'])){
//Best practice is to encrypt the cookies or not use cookies
$key = base64_decode(ENCRYPTION_KEY);
$iv = base64_decode(IV);
$encrypt = new Encrypt($key,$iv,DEFAULT_TIME_ZONE);
// Create TwitteroAuth object with app key/secret and token key/secret from default phase
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// get access token from twitter
try{
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
}
catch(Exception $e){
header("HTTP/1.0 400 Error");
echo "\n\nFailed retrieving access token: " .$e->getMessage();
exit;
}
//Add a credentials validation request. Added v2.0 by AndyG
try{
$content = $connection->get('account/verify_credentials','');
}
catch(Exception $e){
$error = $e->getMessage();
}
// save token
$_SESSION['oauth_access_token'] = $access_token;
// 1 year
$cookie_life = time() + 31536000;
if($content != null && $content->screen_name != ""){
$token = base64_encode( $encrypt->encrypt($access_token['oauth_token']));
$token_secret = base64_encode( $encrypt->encrypt($access_token['oauth_token_secret']));
//Update array with new encrypted values
$access_token["oauth_token"] = $token;
$access_token["oauth_token_secret"] = $token_secret;
// echo "\n\n".var_dump($access_token); //for testing
// set cookie
setcookie(OAUTH_COOKIE, json_encode($access_token), $cookie_life, '/', OAUTH_COOKIE_DOMAIN);
//header('Location: ./callback.php');
echo "<html><head><title>Valid Verification</title><body bgcolor='#C0C0C0'>";
echo "<style type='text/css'>body{font-family:sans-serif;}</style>";
echo "<table width='100%'><tr bgcolor='#FFFFFF'><td>";
echo "<a href='http://www.esri.com'><img src='edn.png' style='border-style:none' alt='ESRI Developer Network' /></a>";
echo "</td></tr></table>";
echo "<h2>Welcome: <img src='".$content->profile_image_url."'></img> @".$content->screen_name."</h2>";
echo "<h4>You have successfully authenticated with Twitter. </h4>" ;
echo "<h4>It is okay to close this page and return to the application.</h4>";
echo "<script language=\"JavaScript\">\n";
echo "if(window.opener && window.opener.getTokens){";
echo "window.opener.getTokens(\"".$_SESSION['oauth_token'].",".$_SESSION['oauth_token_secret']."\");}";
//You can also have the app automatically close the window via self.close, as shown below
//echo "self.close();";
echo "</script>";
echo "</body></html>";
}
else{
header("HTTP/1.0 400 Error");
echo "\n\nFailed to validate credentials. ".$error;
exit;
}
exit;
}
else{
// redirect
if(isset($_SESSION['oauth_referrer'])){
header('Location: '.$_SESSION['oauth_referrer']);
}
else{
header('Location: '.OAUTH_CALLBACK);
}
exit;
}
}