Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Twitter OAuth无法检索访问令牌_Php_Authentication_Twitter_Oauth - Fatal编程技术网

Php Twitter OAuth无法检索访问令牌

Php Twitter OAuth无法检索访问令牌,php,authentication,twitter,oauth,Php,Authentication,Twitter,Oauth,我正在用PHP构建一个Twitter应用程序,使用TwitterOAuth库进行用户身份验证 我能够将用户重定向到Twitter进行身份验证,并接收oauth令牌、oauth secret和oauth验证器,但是我无法完成身份验证的最后一步,即获取访问令牌。 我正在localhost上进行开发,并使用设置了回调路径 我的应用程序具有读写权限 这是我的密码: index.php <?php session_start(); include "twitteroauth/aut

我正在用PHP构建一个Twitter应用程序,使用TwitterOAuth库进行用户身份验证

我能够将用户重定向到Twitter进行身份验证,并接收oauth令牌、oauth secret和oauth验证器,但是我无法完成身份验证的最后一步,即获取访问令牌。 我正在localhost上进行开发,并使用设置了回调路径

我的应用程序具有读写权限

这是我的密码:

index.php

<?php
    session_start();
    include "twitteroauth/autoload.php";
    include "oauthConsts.php";

    use Abraham\TwitterOAuth\TwitterOAuth;

    // request authentication tokens
    $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $oauth->getRequestToken(
                     'http://127.0.0.1/TwitterApp/update.php');

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

    if($oauth->getLastHttpCode()==200){
        // Let's generate the URL and redirect
        $url = $oauth->getAuthorizeURL($request_token['oauth_token']);
        header('Location: '. $url);
    } else {
        echo "something went wrong";
    }

?>
 <?php
        session_start();
        include "twitteroauth/autoload.php";
        include "oauthConsts.php";

        use Abraham\TwitterOAuth\TwitterOAuth;

        if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token'])
            && !empty($_SESSION['oauth_secret'])) {
            $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 
                         $_SESSION['oauth_token'], $_SESSION['oauth_secret']);
            $access_token = $oauth->oauth("oauth/access_token", 
                          array("oauth_verifier" => $_GET['oauth_verifier']));

            $_SESSION['access_token'] = $access_token;

        }
        else {
            header('Location: index.php');
        }
    ?>

事实证明,我的问题是由会话和回调url引起的

我是通过localhost/path/to/file访问索引页面的,但twitter在用户身份验证后重定向到127.0.0.1/path/to/file,这意味着存储在localhost上的会话数据在127.0.0.1上无法访问

使用127.0.0.1而不是localhost访问索引页解决了这个问题