public function actionAuth() { //$sa = SpeedOpenOAut define("SERVER_ONLINE", FALSE); define("SPEED_APPKEY", '100001'); define("SPEED_APPSECRET", '543774710dcc91a7e52428bf02ac8c41'); //define( "SPEED_CALLBACK_URL" , 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"] . 'callback.php' ); define("SPEED_CALLBACK_URL", 'http://postmill.local:8080/index.php?r=site/auth'); // $oauth = new \app\trdapi\mls\SpeedOpenOAuth( SPEED_APPKEY , SPEED_APPSECRET ); // $oauth = new SpeedOpenOAuth( SPEED_APPKEY , SPEED_APPSECRET ); $oauth = new SpeedOpenOAuth(Yii::$app->params['oauth']['appid'], Yii::$app->params['oauth']['appsecretkey']); $keys = []; $keys['redirect_uri'] = SPEED_CALLBACK_URL; if (isset($_REQUEST['code'])) { $keys['code'] = $_REQUEST['code']; } // var_dump($keys); try { $token = $oauth->getAccessToken('code', $keys); // var_dump($token); } catch (Exception $e) { $msg = $e->getMessage(); } if ($token && $token['code'] == 200) { $_SESSION['token'] = $token; setcookie('speed_token_' . $oauth->client_id, http_build_query($token)); } $this->goBack(); // die; // sso end }
public function actionAuth() { $oauth = new SpeedOpenOAuth(Yii::$app->params['oauth']['appid'], Yii::$app->params['oauth']['appsecretkey']); $callbackUrl = 'http://' . Yii::$app->params['oauth']['domain'] . '/index.php?r=site/auth'; $keys = []; $keys['redirect_uri'] = $callbackUrl; if (isset($_REQUEST['code'])) { $keys['code'] = $_REQUEST['code']; try { $token = $oauth->getAccessToken('code', $keys); // echo "<br><br><br>"; // echo $oauth->getApiHost(); // echo "<br><br>request<br>"; // var_dump($_REQUEST); // echo "<br><br>tokens<br>"; // var_dump($token); // die; } catch (Exception $e) { $msg = $e->getMessage(); } if ($token && $token['code'] == 200 && isset($token['access_token'])) { $_SESSION['token'] = $token; //setcookie( 'speed_token_'.$oauth->client_id, http_build_query($token) ); setcookie('speed_token_' . Yii::$app->params['oauth']['appid'], http_build_query($token)); $user = new \app\models\User(); $user->accessToken = $token["access_token"]; $user->authKey = ''; $detail = $oauth->getUserInfo($user->accessToken); if ($detail && isset($detail['code']) && $detail['code'] == 200 && isset($detail['data']) && isset($detail['expires_in'])) { setcookie('speed_user', http_build_query($detail['data']), $detail['expires_in']); $user->id = $detail['data']['id']; $user->username = $detail['data']['name']; $user->mail = $detail['data']['mail']; echo "<br><br>user info get successfully<br>"; } Yii::$app->user->login($user); // Yii::$app->user->login( // Yii::$app->user->loginByAccessToken // Yii::$app->user->loginByCookie return $this->goHome(); } } return Yii::$app->user->loginRequired(); }