Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 4.02 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Error to login via twitter (modx)
  2. $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
  3.     'oauth_verifier' => $_REQUEST['oauth_verifier']
  4.   ));
  5.  
  6.   if ($code == 200) {
  7.        
  8. <?php
  9.  
  10. require MODX_CORE_PATH.'components/twPost/tmhOAuth.php';
  11. require MODX_CORE_PATH.'components/twPost/tmhUtilities.php';
  12.  
  13. $tmhOAuth = new tmhOAuth(array(
  14.   'consumer_key'    => '*************',
  15.   'consumer_secret' => '**************',
  16. ));
  17.  
  18. $contexts = empty($contexts) ? array($modx->context->get('key')) : explode(',', $contexts);
  19.  
  20. if(isset($_SESSION['access_token']) ){
  21.     $tmhOAuth->config['user_token']  = $_SESSION['access_token']['oauth_token'];
  22.     $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
  23.  
  24.     $code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
  25.     if ($code == 200) {
  26.         $user_data = json_decode($tmhOAuth->response['response']);
  27.         $moduser = $modx->getObject('modUser',  array('remote_key:=' => $user_data->id, 'remote_key:!=' => null));
  28.         if(empty($moduser)){
  29.             $homet = '';
  30.             if(!empty($user_data->location)){
  31.                 $hometownAr = explode(',',$user_data->location);
  32.                 $homet = $hometownAr[0];
  33.             }
  34.  
  35.             $moduser = $modx->newObject('modUser');
  36.             $moduser->set('username', $user_data->screen_name);
  37.             $moduser->set('active', true);
  38.             $moduser->set('remote_key', $user_data->id);
  39.             $moduser->set('remote_data', (array)$user_data);
  40.  
  41.  
  42.             $profile = $modx->newObject('modUserProfile');
  43.             $profile->set('email', $user_data->screen_name.'@twitter.com');
  44.             $profile->set('fullname', $user_data->name);
  45.             $profile->set('city', $homet);
  46.             $profile->set('photo', $user_data->profile_image_url);
  47.  
  48.             $moduser->addOne($profile, 'Profile');
  49.             $saved = $moduser->save();
  50.         }
  51.  
  52.         $_SESSION['oauth_token']  = $_SESSION['access_token']['oauth_token'];
  53.         $_SESSION['oauth_token_secret'] = $_SESSION['access_token']['oauth_token_secret'];
  54.  
  55.         $moduser->set('active',1);
  56.         $moduser->save();
  57.  
  58.         foreach ($contexts as $context) {
  59.             $moduser->addSessionContext($context);
  60.         }
  61.  
  62.         $url = $_SESSION['back_url_login'];
  63.         if(empty($url)) $url = "/";
  64.         unset($_SESSION['back_url_login']);
  65.         $modx->sendRedirect($url);        
  66.     } else {
  67.         $_SESSION['error_upform'] = 'Login error. Please try again. Main';
  68.         $modx->sendRedirect('/');
  69.     }
  70.  
  71. } elseif (isset($_REQUEST['oauth_verifier'])) {
  72.   $tmhOAuth->config['user_token']  = $_SESSION['oauth']['oauth_token'];
  73.   $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
  74.  
  75.   $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
  76.     'oauth_verifier' => $_REQUEST['oauth_verifier']
  77.   ));
  78.  
  79.   if ($code == 200) {
  80.     $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
  81.     unset($_SESSION['oauth']);
  82.     $modx->sendRedirect('/?id=12');
  83.   } else {
  84.         $_SESSION['error_upform'] = 'Login error. Please try again. verifier. | '.$tmhOAuth->response['response'];
  85.         $modx->sendRedirect('/');
  86.   }
  87. // start the OAuth dance
  88. }elseif ( isset($_REQUEST['auth']) && $_REQUEST['auth']=='1' ) {
  89.     $_SESSION['back_url_login'] = $_SERVER['HTTP_REFERER'];
  90.  
  91.     $params = array(
  92.         'oauth_callback'     => '/?id=12'
  93.     );
  94.  
  95.     $params['x_auth_access_type'] = 'write';
  96.     //$params['x_auth_access_type'] = 'read';
  97.  
  98.     $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
  99.  
  100.     if ($code == 200) {
  101.         $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
  102.         $method = 'authorize'; $force  = '';
  103.         $authurl = $tmhOAuth->url("oauth/{$method}", '') .  "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
  104.         $modx->sendRedirect($authurl);
  105.   } else {
  106.         $_SESSION['error_upform'] = 'Login error. Please try again.';
  107.         $modx->sendRedirect('/');
  108.   }
  109. }