dongtangu6889 2011-06-02 16:34
浏览 239
已采纳

Twitter身份验证/ PHP Oauth:要求用户每次尝试进行身份验证时都要对应用程序进行实时操作

The code itself is pretty basic. I am trying to allow user to connect to Twitter and later to use Twitter as a mean of authentication (afaik, this is called OpenId). However, the problem is that every time when user executes the code he is asked to reallow the application – instead I expect it to return some sort of variable indicating that user is already connected to the app.

$oauth          = new OAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
$callback_url   = 'http://guubo.com/connect/1';

try
{
    if(empty($_GET['oauth_token']))
    {
        unset($_SESSION['hp']['twitter']);
    }

    if(empty($_SESSION['hp']['twitter']['oauth_token_secret']))
    {
        $access_token   = $oauth->getRequestToken('https://api.twitter.com/oauth/request_token', $callback_url);

        //die(var_dump( $access_token ));

        $_SESSION['hp']['twitter']['oauth_token_secret']    = $access_token['oauth_token_secret'];

        header('Location: https://api.twitter.com/oauth/authorize?oauth_token=' . $access_token['oauth_token']);

        exit;
    }
    elseif(!empty($_GET['oauth_token']))
    {
        $oauth->setToken($_GET['oauth_token'], $_SESSION['hp']['twitter']['oauth_token_secret']);

        unset($_SESSION['hp']['twitter']);

        $access_token_info  = $oauth->getAccessToken('https://api.twitter.com/oauth/access_token');

        $db->exec("INSERT INTO `user_tokens` (`user_id`, `network_id`, `oauth_token`, `oauth_token_secret`) VALUES ({$db->quote($user['id'])}, {$db->quote($network['id'])}, {$db->quote($access_token_info['oauth_token'])}, {$db->quote($access_token_info['oauth_token_secret'])})");

        $response_array = array();

        parse_str($oauth->getLastResponse(), $last_response);

        #die(var_dump( $last_response[''] ));
    }
}
catch(OAuthException $e)
{
    echo $e->getMessage();

    exit;
}
  • 写回答

1条回答 默认 最新

  • doupeng8494 2011-06-02 18:43
    关注

    Take a look at this document regarding the "Sign in with Twitter" flow, which it looks like you are trying to implement. The one thing I notice in your code is that you are calling oauth/authorize where you should be calling oauth/authenticate. Here's a relevant excerpt:

    The normal [OAuth] flow dictates that applications send request tokens to oauth/authorize in Twitter's implementation of the OAuth Specification. To take advantage of "Sign in with Twitter", applications should send request tokens received in the oauth_token parameter to oauth/authenticate instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目