Twitter authentication : 403 Forbidden: The server

2020-07-24 05:05发布

问题:

I am trying to authenticate user using OAuth and retrieve the user data. When the user is not signed into twitter the authentication works and I am able to get the user details. But if the user is already signed in on twitter I am getting this error message '403 Forbidden: The server understood the request, but is refusing to fulfill it.' . In some posts they said to make all the requests through https instead of http. That I have done. I have downloaded the code for authentication from 'http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/' . Please help.

$twitterOauthObj    =   new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret);
if(!isset($_GET['oauth_token'])){

    $requestTokenArray  =   $twitterOauthObj->getRequestToken($callback_url);
    $requestToken       =   $requestTokenArray['oauth_token'];
    $tokenSecret        =   $requestTokenArray['oauth_token_secret'];

    $authorizeUrl       =   $twitterOauthObj->getAuthorizeURL($requestToken);
    $response       =   $twitterOauthObj->oAuthRequest($authorizeUrl, 'GET', $requestTokenArray);
    print_r($response);
} else{

    $oauthToken     =   $_GET['oauth_token'];
    $requestToken       =   $oauthToken;
    $oauthVerifier      =   $_GET['oauth_verifier'];
    $accessTokenArray   =   $twitterOauthObj->getAccessToken($oauthVerifier, $oauthToken);
        $oauthToken     =   $accessTokenArray['oauth_token'];
    $oauthTokenSecret   =   $accessTokenArray['oauth_token_secret'];
    $userTwitterId      =   $accessTokenArray['user_id'];
    $screenName     =   $accessTokenArray['screen_name'];
 }

回答1:

Ok. I got it working. I was not redirecting to authorize url correctly. Here is the modified code.

if(!isset($_GET['oauth_token'])){

    $requestToken   =   $twitterOauthObj->getRequestToken();
    $_SESSION["oauth_token"] = $requestToken["oauth_token"];
    $_SESSION["oauth_token_secret"] = $requestToken["oauth_token_secret"];

    header("Location:".$twitterOauthObj->getAuthorizeURL($requestToken["oauth_token"]));

}

Thank you guys for all your valuable suggestions. Really appreciate it!!