public function run() { require_once Yii::getPathOfAlias('ext') . "/OAuth/renren/rennclient/RennClient.php"; $config = OAuth::getConf('renren'); $rennClient = new RennClient($config['app_key'], $config['app_secret']); // 处理code -- 根据code来获得token if (isset($_REQUEST['code'])) { // 验证state,防止伪造请求跨站攻击 $state = $_REQUEST['state']; if (empty($state) || $state !== Yii::app()->session['renren_state']) { throw new CHttpException(500, 'Error: Illegal Request'); } unset(Yii::app()->session['renren_state']); // 获得code $code = $_REQUEST['code']; $redirect_uri = $config['callback']; try { // 根据code来获得token $token = $rennClient->authWithAuthorizationCode($code, $redirect_uri); } catch (RennException $e) { throw new CHttpException(500, 'Error:' . $e->getMessage()); } } else { throw new CHttpException(500, 'Auth Failed'); } if ($token) { // 获得保存的token $rennClient->authWithStoredToken(); // 获得用户接口 $user_service = $rennClient->getUserService(); // 获得用户信息 $user = $user_service->getUser(null); if (!$user) { throw new CHttpException('500', Yii::t('common', 'Login Failed') . '(get userinfo failed)'); } // 获取accessToken $access_token = $token->accessToken; $openid = $user['id']; //查看是否已绑定 $bind = OAuthRenren::model()->findByPk($openid); //数据 $data = array('type' => 'renren', 'access_token' => $access_token, 'openid' => $openid, 'uid' => $bind ? $bind->uid : 0, 'username' => $user['name'], 'avatar' => $user['avatar'][1]['url']); //绑定注册 $this->controller->bind_register($bind, $data); } else { $this->controller->message('error', Yii::t('common', 'Login Failed') . '(renren_x_0000)', $this->createUrl('user/login')); } }
<?php session_start(); include_once 'config.php'; include_once '../rennclient/RennClient.php'; $renn_client = new RennClient(APP_KEY, APP_SECRET); $renn_client->setDebug(DEBUG_MODE); // 获得保存的token $renn_client->authWithStoredToken(); // 获得用户接口 $user_service = $renn_client->getUserService(); // 获得指定用户 $user = $user_service->get(431695399); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>人人api2 demo</title> </head> <body> <!-- 用户头像 --> <img src="<?php echo $user['avatar'][1]['url']; ?> " title="头像" alt="头像" border="0" /> <br /> <!-- 用户名 --> <?php