通过twitter登录时出错(modx)

时间:2012-07-17 08:02:06

标签: twitter sdk oauth modx-revolution

我在modx revo中有一个网站。通过twitter开发自己的授权。使用SDK(https://github.com/themattharris/tmhOAuth/)。工作原理:对Twitter的请求。返回twitter后检查网站上是否有新用户。如果是,请创建用户并在站点上创建会话。如果没有,只需创建一个会话。

但这不能正常运作。 1.一旦他对请求提出错误

  $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
    'oauth_verifier' => $_REQUEST['oauth_verifier']
  ));

  if ($code == 200) {
  1. 下次没有任何事情发生!

  2. 下次完美登录。

  3. 每次都会循环执行这些操作。 1-2-3步骤。

    有什么问题?什么不起作用?

    插件的代码:

    <?php
    
    require MODX_CORE_PATH.'components/twPost/tmhOAuth.php';
    require MODX_CORE_PATH.'components/twPost/tmhUtilities.php';
    
    $tmhOAuth = new tmhOAuth(array(
      'consumer_key'    => '*************',
      'consumer_secret' => '**************',
    ));
    
    $contexts = empty($contexts) ? array($modx->context->get('key')) : explode(',', $contexts);
    
    if(isset($_SESSION['access_token']) ){
        $tmhOAuth->config['user_token']  = $_SESSION['access_token']['oauth_token'];
        $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
    
        $code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
        if ($code == 200) {
            $user_data = json_decode($tmhOAuth->response['response']);
            $moduser = $modx->getObject('modUser',  array('remote_key:=' => $user_data->id, 'remote_key:!=' => null));
            if(empty($moduser)){
                $homet = '';
                if(!empty($user_data->location)){
                    $hometownAr = explode(',',$user_data->location);
                    $homet = $hometownAr[0];
                }
    
                $moduser = $modx->newObject('modUser');
                $moduser->set('username', $user_data->screen_name);
                $moduser->set('active', true);
                $moduser->set('remote_key', $user_data->id);
                $moduser->set('remote_data', (array)$user_data);
    
    
                $profile = $modx->newObject('modUserProfile');
                $profile->set('email', $user_data->screen_name.'@twitter.com');
                $profile->set('fullname', $user_data->name);
                $profile->set('city', $homet);
                $profile->set('photo', $user_data->profile_image_url);
    
                $moduser->addOne($profile, 'Profile');
                $saved = $moduser->save();
            }
    
            $_SESSION['oauth_token']  = $_SESSION['access_token']['oauth_token'];
            $_SESSION['oauth_token_secret'] = $_SESSION['access_token']['oauth_token_secret'];
    
            $moduser->set('active',1);
            $moduser->save();
    
            foreach ($contexts as $context) {
                $moduser->addSessionContext($context);
            }
    
            $url = $_SESSION['back_url_login'];
            if(empty($url)) $url = "/";
            unset($_SESSION['back_url_login']);
            $modx->sendRedirect($url);        
        } else {
            $_SESSION['error_upform'] = 'Login error. Please try again. Main';
            $modx->sendRedirect('/');
        }
    
    } elseif (isset($_REQUEST['oauth_verifier'])) {
      $tmhOAuth->config['user_token']  = $_SESSION['oauth']['oauth_token'];
      $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
    
      $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
        'oauth_verifier' => $_REQUEST['oauth_verifier']
      ));
    
      if ($code == 200) {
        $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
        unset($_SESSION['oauth']);
        $modx->sendRedirect('/?id=12');
      } else {
            $_SESSION['error_upform'] = 'Login error. Please try again. verifier. | '.$tmhOAuth->response['response'];
            $modx->sendRedirect('/');
      }
    // start the OAuth dance
    }elseif ( isset($_REQUEST['auth']) && $_REQUEST['auth']=='1' ) {
        $_SESSION['back_url_login'] = $_SERVER['HTTP_REFERER'];
    
        $params = array(
            'oauth_callback'     => '/?id=12'
        );
    
        $params['x_auth_access_type'] = 'write';
        //$params['x_auth_access_type'] = 'read';
    
        $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
    
        if ($code == 200) {
            $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
            $method = 'authorize'; $force  = '';
            $authurl = $tmhOAuth->url("oauth/{$method}", '') .  "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
            $modx->sendRedirect($authurl);
      } else {
            $_SESSION['error_upform'] = 'Login error. Please try again.';
            $modx->sendRedirect('/');
      }
    }
    

1 个答案:

答案 0 :(得分:0)

添加

'oauth_token' => $_REQUEST['oauth_token']

根据您的要求。像这样:

$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
    'oauth_verifier' => $_REQUEST['oauth_verifier'],
    'oauth_token' => $_REQUEST['oauth_token']
));

如果没有这条线,twitter根本就不适合我。