我在 modx revo 有一个网站。通过推特开发了自己的授权。使用了 SDK (https://github.com/themattharris/tmhOAuth/)。它是如何工作的:对 Twitter 的请求。返回推特后,检查网站上是否有新用户。如果是这样,请创建一个用户并在该站点上创建一个会话。如果没有,只需创建一个会话。
但这不能正常工作。1. 一旦他对请求给出错误
$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
));
if ($code == 200) {
下一次,什么都没有发生!
下次登录时完美执行。
这些动作每次都循环执行。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('/');
}
}