- Error to login via twitter (modx)
- $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
- 'oauth_verifier' => $_REQUEST['oauth_verifier']
- ));
- if ($code == 200) {
- <?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('/');
- }
- }