开发者

Abraham Williams Twitter oAuth PHP Callback Issue

开发者 https://www.devze.com 2023-03-17 03:33 出处:网络
I am building an app using Abraham William\'s TwitterOAuth Package. The app is working fine, except I am running into one problem.

I am building an app using Abraham William's TwitterOAuth Package. The app is working fine, except I am running into one problem.

About the 25% of the users who try to install the app cannot do so. They verify the application on the Twitter Page and click "Sign In" (on Twitter's website). When they are referred back to the Callback.php page, it gives them a blank page. I cannot figure out what is wrong, because it works for most users. Any ideas?

Here is the callback.php code:

<?php

require_once("config_db.php");

session_start();
// Include class & create
require_once("consumer-keys.ph开发者_开发技巧p");
require_once("twitteroauth/twitteroauth/twitteroauth.php");
// User has selected to DENY access
if(!empty($_GET["denied"])) {
  // could re-direct or display cancelled view/template
  // we're just echoing out a message
  echo "No deal! <a href='login.php'>Try again?</a>";
  die();
}

// User has selected to ALLOW access for given token
if($_GET["oauth_token"] == $_SESSION["oauth_token"]) {
  // Use generated request tokens (from session) to construct object
  $tOAuth = new TwitterOAuth($consumerKey, $consumerSecret, $_SESSION["oauth_token"], $_SESSION["oauth_token_secret"]);
  // Retrieve access token from Twitter
  $accessToken = $tOAuth->getAccessToken();

    //echo "RECEIVED TOKENS<br>";
  // Check we have valid response
  if(is_numeric($accessToken["user_id"]))  {
    // Save the access tokens to a DB (we're using a session)
    $_SESSION["access_token"] = $accessToken;

    //GET CREDENTIALS VIA API
    $credentials = $tOAuth->get('account/verify_credentials');

    //echo "HAS NUMERIC ID<br>";

    //ISAAC ADD DB INFO
    $query = "SELECT * FROM `t4i_app`.`users_twitter` WHERE user_id = '".$_SESSION["access_token"]["user_id"]."'";
    $rs = mysql_query($query);
    if(mysql_num_rows($rs)) {

        //update query
        //echo "user already exists..need to update";
        $query = "UPDATE  `t4i_app`.`users_twitter` SET  `token` =  '".$_SESSION["access_token"]["oauth_token"]."', `secret` =  '".$_SESSION["access_token"]["oauth_token_secret"]."', `screen_name` =  '".$credentials->screen_name."', `followers` = '".$credentials->followers_count."'  WHERE  `users_twitter`.`user_id` = '".$_SESSION["access_token"]["user_id"]."';";
        mysql_query($query);
    } else {

        $query = "INSERT INTO  `t4i_app`.`users_twitter` (`id` ,`screen_name` ,`token` ,`secret`, `user_id`, `followers`, `referer` ,`last_update` ,`date_registered`) VALUES (NULL, '".$credentials->screen_name."', '".$_SESSION["access_token"]["oauth_token"]."', '".$_SESSION["access_token"]["oauth_token_secret"]."', '".$_SESSION["access_token"]["user_id"]."', '".$credentials->followers_count."', '0', CURRENT_TIMESTAMP , CURRENT_TIMESTAMP);";
        mysql_query($query);

    }
    //echo $query;
    //echo mysql_error();
    //print_r($_SESSION["access_token"]);
    $message = array('status' => 'Test OAuth update. #testoauth');
    $test = $tOAuth->post('statuses/update', array('status' => 'I just added the new APPNAME Twitter Application http://bit.ly/sampleurl'));
    //echo "POSTED STATUS<br>";


    //END ISAAC




    // Remove request token session variables
    unset($_SESSION["oauth_token"]);
    unset($_SESSION["oauth_token_secret"]);
    // Redirect to main page
    header("location: http://www.mydomain.com/welcome.php");
  } else {
    //echo "TRYING TO REDIRECT <br>";

    header("location: http://www.mydomain.com");
  }
}
?>


if ($_GET["oauth_token"] == $_SESSION["oauth_token"])
  • You'll want to put an error_log in the else for this, you're failing without any messages if the oauth_tokens in the get and session don't match. It could be that Twitter isn't responding properly.

  • Also, I would capitalize the Location in the header call. I'm not 100% that every browser will honor it when it's not Location:

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号