TwitterOauth no redirect back to my website


#1

Hey,

im new in developing with oAuth and Twitter :), so i hope you can help me.
I have a little bit trouble with the twitteroauth library, but i think it is more a configuration problem.

So, i have setup a Twitter App with the Domain and Callback URL.

At first my login Code:

public function loginAction() {
    session_start();

    if (isset($this->settings['consumerKey']) && isset($this->settings['consumerSecret'])) {

        $consumerKey = $this->settings['consumerKey'];
        $consumerSecret = $this->settings['consumerSecret'];
        $callback = $_SERVER["HTTP_HOST"] . '/callback.php';

        $connection = new TwitterOAuth($consumerKey, $consumerSecret);
        $request_token = $connection->oauth("oauth/request_token", array('oauth_callback' => $callback));
        
        $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));

        $_SESSION['oauth_token'] = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
        
        return 'foorbar';
        $this->view->assign('url', $url);
    }
}

That works great an i will redirected to Twitter, can login and verify. All right.
This code will be called:

session_start();
    $request_token = [];
    $request_token['oauth_token'] = $_SESSION['oauth_token'];
    $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

    $consumerKey = $this->settings['consumerKey'];
    $consumerSecret = $this->settings['consumerSecret'];

    $connection = new TwitterOAuth($consumerKey, $consumerSecret, $request_token['oauth_token'],    $request_token['oauth_token_secret']);
    $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
    
    $_SESSION['status'] = 'verified';
    $_SESSION['access_token'] = $access_token;
    var_dump($access_token);

The problem ist, that the redirect to my website dont work and i dont know why. Everytime twitter will me show: “This page does not exist.” with this URL:

https://api.twitter.com/oauth/[myDomain]/callback.php?oauth_token=[token]&oauth_verifier=[token]

If i copy my all, after “oauth/” and paste to a new browser tab, it will open the site and show the correct access token. So, i think on this side all will be ok. But i dont know, why twitter dont redirect back to my website.

I hope you can help me.
best regards
daniel


#2

According to the PHP documentation $_SERVER["HTTP_HOST"] results in something like www.example.com. You need to tell the browser what protocol (http:// or https://) to use though.

The quick fix is to do:

$callback = 'http://' . $_SERVER["HTTP_HOST"] . '/callback.php';

If your working in different environments you would want to check the port and switch between https:// and http:// depending on your needs.


#3

Thanks a lot. That works :blush: Sry for this stupid question.