/** * Generate an access token (oauth token and oauth secret) from Twitter OAuth * * @param string $oauth_token * @param string $oauth_verifier * @return array * @throws \Exception */ protected function getAccessToken($oauth_token, $oauth_verifier) { $data_token = $this->client->oauth('oauth/access_token', array('oauth_verifier' => $oauth_verifier, 'oauth_token' => $oauth_token)); if (!array_key_exists('oauth_token', $data_token) || !array_key_exists('oauth_token_secret', $data_token)) { throw new \Exception("OAuth token not confirmed"); } return $data_token; }
/** * @return void * @throws \Exception */ public function execute() { $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $data = $this->getRequest()->getParams(); /* Get temporary credentials from session. */ $request_token = []; $request_token['oauth_token'] = $this->scopeConfig->getValue('sama_twitterfeed/oauth/token', $storeScope); $request_token['oauth_token_secret'] = $this->scopeConfig->getValue('sama_twitterfeed/oauth/token_secret', $storeScope); /* If denied, bail. */ if (isset($data['denied'])) { throw new Exception("Twitter denied permission"); } /* If the oauth_token is not what we expect, bail. */ if (isset($data['oauth_token']) && $request_token['oauth_token'] !== $data['oauth_token']) { throw new Exception("Unexpected Oauth token"); } /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ $connection = new TwitterOAuth($this->_oAuthkey, $this->_oAuthsecret, $request_token['oauth_token'], $request_token['oauth_token_secret']); /* Request access tokens from twitter */ $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $data['oauth_verifier'])); /* If HTTP response is 200 continue otherwise send to connect page to retry */ if (200 == $connection->getLastHttpCode()) { $this->_objectManager->get('Magento\\Framework\\App\\MutableScopeConfig')->setValue('sama_twitterfeed/oauth/access_token', $access_token); $this->_objectManager->get('Magento\\Framework\\App\\MutableScopeConfig')->setValue('sama_twitterfeed/oauth/token_secret', null); $this->_objectManager->get('Magento\\Framework\\App\\MutableScopeConfig')->setValue('sama_twitterfeed/oauth/token', null); } else { throw new Exception("Twitter Oauth API status code: {$connection->getLastHttpCode()}"); } return; }
private function _callbackFlow() { // 不正認証防止用に認証リクエスト用トークンをチェック if ($_GET['oauth_token'] !== $_SESSION['tmp_twitter_oauth_token']) { echo "Invalid Token!"; exit; } // twitterのユーザー情報の取得 $conn = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $_SESSION['tmp_twitter_oauth_token'], $_SESSION['tmp_twitter_oauth_token_secret']); $tokens = $conn->oauth('oauth/access_token', ['oauth_verifier' => $_GET['oauth_verifier']]); //取得したtwitterユーザー情報をDBに登録もしくは更新 $user = new \App\Model\User(); if ($user->existsTwitterUser($tokens['user_id'])) { if (!$user->updateTwitterUser($tokens)) { echo "update error!"; exit; } } else { if (!$user->createTwitterUser($tokens)) { echo "create error!"; exit; } } //ログイン処理 session_regenerate_id(true); $_SESSION['user'] = $user->findTwitterUser(['tw_user_id' => $tokens['user_id']]); //認証リクエスト用トークンはもう使わないので消す unset($_SESSION['tmp_twitter_oauth_token']); unset($_SESSION['tmp_twitter_oauth_token_secret']); header('Location: ' . SITE_URL); exit; }
function GetFollowersIds() { /*session_start(); $connection = new TwitterOAuth("CONSUMER_KEY", "CONSUMER_SECRET"); $_SESSION['oauth_token'] = $token = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; $url = $connection->getAuthorizeURL($token); $connection = new TwitterOAuth("CONSUMER_KEY", "CONSUMER_SECRET", $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); $_SESSION['access_token'] = $access_token; unset($_SESSION['oauth_token']); unset($_SESSION['oauth_token_secret']); $connection = new TwitterOAuth('CONSUMER_KEY', 'CONSUMER_SECRET', $access_token['oauth_token'], $access_token['oauth_token_secret']); $content = $connection->get('followers/ids'); var_dump($content);*/ $connection = new TwitterOAuth("CONSUMER_KEY", "CONSUMER_SECRET"); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => 'CONSUMER_KEY')); var_dump($access_token); }
public function getAuthorizationUrl(array $options = []) { $connection = new TwitterOAuth($this->clientId, $this->clientSecret); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => $this->redirectUri)); $options["session"]->write('twitter.request_token', $request_token); $url = $connection->url('oauth/authenticate', array('oauth_token' => $request_token['oauth_token'])); return $url; }
public function getAccessToken() { if (!isset($_SESSION['access_token']) && !isset($_SESSION['access_token_secret'])) { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_COOKIE['oauth_token'], $_COOKIE['oauth_token_secret']); $access_token = $connection->oauth('oauth/access_token', array('oauth_verifier' => $_GET['oauth_verifier'])); $_SESSION['access_token'] = $access_token['oauth_token']; $_SESSION['access_token_secret'] = $access_token['oauth_token_secret']; } }
function GenerateLoginLink() { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); return $url; }
public function getUserInformation($user_oauth_token, $user_oauth_verifier) { $connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $user_oauth_token, $_SESSION['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $user_oauth_verifier]); $connection2 = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); $user = $connection2->get("account/verify_credentials"); $this->response['data'] = ['imagePath' => str_replace('_normal', '', $user->profile_image_url), 'imageThumb' => $user->profile_image_url, 'twitterId' => $user->id, 'name' => $user->name]; return $this->response; }
public function twitter() { $connection = new TwitterOAuth(Config::get('twitter.appid'), Config::get('twitter.secret')); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => Config::get('twitter.callback'))); $_SESSION['login_type'] = "twitter"; $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); return redirect($url); }
public function callbackTwitter() { if (isset($_REQUEST['oauth_verifier'], $_REQUEST['oauth_token']) && $_REQUEST['oauth_token'] == Session::get('oauth_token')) { $request_token = []; $request_token['oauth_token'] = Session::get('oauth_token'); $request_token['oauth_token_secret'] = Session::get('oauth_token_secret'); $connection = new TwitterOAuth(config('socialpack.composer_key'), config('socialpack.composer_secret'), $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); Session::put('access_token', $access_token); return redirect('/socialpacks/twitter'); } }
/** * Obtain an access token (before logging in to twitter access token). * * @see https://dev.twitter.com/web/sign-in/implementing * @param string * @return mixed */ public function getAccessToken($sOathVerifier) { if ($sOathVerifier) { $this->_initializeTwitterOauth(self::OAUTH_VERIFIER); $aAccessToken = $this->_oTwitterOAth->oauth('oauth/access_token', ['oauth_verifier' => $sOathVerifier]); if ($this->_oTwitterOAth->getLastHttpCode() === 200) { AppSessionHandler::i()->set('access_token', $aAccessToken); return $aAccessToken; } } return false; }
private function _redirectFlow() { $conn = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); //リクエストトークンをセットする、コールバックURKを渡す $tokens = $conn->oauth('oauth/request_token', ['oauth_callback' => CALLBACK_URL]); // 後で利用できるようにsessionに保存する $_SESSION['oauth_token'] = $tokens['oauth_token']; $_SESSION['oauth_token_secret'] = $tokens['oauth_token_secret']; // redirect アカウントの利用を許可しますか?へ飛ばす $authorizeUrl = $conn->url('oauth/authorize', ['oauth_token' => $tokens['oauth_token']]); header('Location: ' . $authorizeUrl); exit; }
public function getFinalToken($oauth_token, $oauth_verifier) { $request_token = []; $request_token['oauth_token'] = Session::get('oauth_token'); $request_token['oauth_token_secret'] = Session::get('oauth_token_secret'); if ($request_token['oauth_token'] !== $oauth_token) { return "Your request timed out"; } $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $oauth_verifier]); Session::put('access_token', $access_token); return "Success"; }
public function getAccessToken() { $request_token = []; $request_token['oauth_token'] = $_SESSION['oauth_token']; $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; if (isset($_GET['oauth_token']) && $request_token['oauth_token'] !== $_GET['oauth_token']) { exit('Error: something went wrong...'); } $connection = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth('oauth/access_token', array("oauth_verifier" => $_GET['oauth_verifier'])); if (empty($access_token)) { exit('Error: invalid access token....'); } return $access_token; }
/** * Completes social login. Is caled after redirect from twitter auth page * * @param array $extrainputs List of POST/GET arguments names */ public function completeLogin($extrainputs = array()) { $request_token = []; $request_token['oauth_token'] = $this->request_token['oauth_token']; $request_token['oauth_token_secret'] = $this->request_token['oauth_token_secret']; $this->logQ('session token ' . print_r($request_token, true), 'twitter'); $this->logQ('extra options ' . print_r($extrainputs, true), 'twitter'); if (isset($extrainputs['oauth_token']) && $request_token['oauth_token'] !== $extrainputs['oauth_token']) { throw new \Exception('Twitter oauth. Somethign went wrong. No token in the session'); } $connection = new TwitterOAuth($this->options['consumer_key'], $this->options['consumer_secret'], $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $extrainputs['oauth_verifier'])); $this->access_token = $access_token; return $this->getUserProfile(); }
public function callback($consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_oauth_verifier) { $oauth_access_token = $oauth_access_token; $oauth_access_token_secret = $oauth_access_oauth_verifier; $consumer_key = "LEqoRF6gLyLPxIFlGDjze5xd0"; $consumer_secret = "c0B582T95BFWUUzR2UnOFqWb2RaDQpQ1BH7qPC0aD7w1cf6hVR"; $connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_access_token_secret)); return $access_token; // print_r($access_token); die; $screen_name = $access_token['screen_name']; $oauth_token = $access_token['oauth_token']; $oauth_secret_token = $access_token['oauth_token_secret']; $twitter_id = $access_token['user_id']; }
/** * Generate Access Token * * Generates an access token using the request token. * * @return true * @throws Exception */ public function accessToken() { $twitter = new TwitterOAuth($this->config->get('twitter', 'consumer'), $this->config->get('twitter', 'secret'), $this->session->getTMP('twitter_request_token')['oauth_token'], $this->session->getTMP('twitter_request_token')['oauth_token_secret']); $twitter->setDecodeJsonAsArray(true); if (filter_input(INPUT_GET, 'oauth_token') && filter_input(INPUT_GET, 'oauth_token') !== $this->session->getTMP('twitter_request_token')['oauth_token']) { // Something weird happened. throw new Exception("OAuth tokens to not match."); } $accessToken = $twitter->oauth('oauth/access_token', array('oauth_verifier' => filter_input(INPUT_GET, 'oauth_verifier'))); if (!array_key_exists('screen_name', $accessToken)) { throw new Exception("Access token did not receive screen_name."); } $this->session->setTMP('twitter_name', $accessToken['screen_name']); $this->session->setTMP('twitter_access_token', $accessToken); return true; }
public function goToLoginPage() { $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); // リクエストトークン取得 $token = $twitter->oauth('oauth/request_token', array('oauth_callback' => CALLBACK_URL)); if (!isset($token['oauth_token'])) { echo "エラー発生"; exit; } $request_token = $token['oauth_token']; session_start(); $_SESSION['request_token_secret'] = $token['oauth_token_secret']; // ログインURLを取得してリダイレクト $url = $twitter->url("oauth/authenticate", array("oauth_token" => $request_token)); header('Location: ' . $url); exit; }
public function action_signInComplete() { $base = URL::base(Request::$initial, TRUE); $request_token = []; $request_token['oauth_token'] = $_SESSION['oauth_token']; $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) { header("Location: " . "{$base}" . "Welcome/login?type=loginFailed"); die; } else { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); $_SESSION['access_token'] = $access_token; header('Location:' . "{$base}" . "Welcome/home"); die; } }
/** * @return void * @throws \Exception */ public function execute() { /* Build TwitterOAuth object with client credentials. */ $connection = new TwitterOAuth($this->_oAuthkey, $this->_oAuthsecret); /* Get temporary credentials. */ $request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $this->_objectManager->get('Magento\\Framework\\Url')->getUrl('twitterfeed/oauth/callback', [])]); /* If last connection failed don't display authorization link. */ if (200 == $connection->getLastHttpCode()) { $this->_objectManager->get('Magento\\Framework\\App\\MutableScopeConfig')->setValue('sama_twitterfeed/oauth/token', $request_token['oauth_token']); $this->_objectManager->get('Magento\\Framework\\App\\MutableScopeConfig')->setValue('sama_twitterfeed/oauth/token_secret', $request_token['oauth_token_secret']); /* Build authorize URL and redirect user to Twitter. */ $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); header('location: ' . $url); exit; } else { throw new Exception("Twitter Oauth API status code: {$connection->getLastHttpCode()}"); } }
public function callback() { // $oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier'); // // if (empty($oauth_verifier) || empty(session::get('tw_oauth_token')) || empty(session::get('tw_oauth_secret'))) { // // something's missing, go and login again // header('Location: ' . tw_url_login); // } session::init(); $request_token = array(); $request_token['oauth_token'] = session::get('tw_oauth_token'); $request_token['oauth_token_secret'] = session::get('tw_oauth_token_secret'); // connect with application token $connection = new TwitterOAuth(tw_consumer_key, tw_consumer_secret, $request_token['oauth_token'], $request_token['oauth_token_secret']); // request user token $token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); // session::set('tw_token',$token); auth::setLogin($token, true, $db = $this->db); session::remove('tw_oauth_token'); session::remove('tw_oauth_token_secret'); header('Location: /'); // // // $twitter = new TwitterOAuth( // tw_consumer_key, // tw_consumer_secret, // $token['oauth_token'], // $token['oauth_token_secret'] // ); // // print_r($token); // // $result = $twitter->get('users/show', array('user_id'=>$token['user_id'])); // // print_r($result); // // $status = $twitter->post( // "statuses/update", [ // "status" => "m2m dene".date("Y-m-d h:i:sa") // ] // ); // // echo ('Created new status with #' . $status->id . PHP_EOL); }
public function signin() { // cek jika token sementara yng sebelumnya sama dengan yang dikirim ke twitter $request_token = []; $request_token['oauth_token'] = $_SESSION['oauth_token']; $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) { // throw error echo "Error."; exit; } // instance twitteroauth dengan temporary request token $connection = new TwitterOAuth(consumer_key, consumer_secret, $request_token['oauth_token'], $request_token['oauth_token_secret']); // get access token $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); // instance twitteroauth dengan temporary request token baru $connection = new TwitterOAuth(consumer_key, consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); // simpan access token $_SESSION['access_token'] = $access_token; // set model dengan access token yang baru $this->setModel(); // simpan nama & screen_name ke session $credential = $this->modelUser->getCredentials(); $_SESSION['nama'] = $credential->name; $_SESSION['screen_name'] = $credential->screen_name; $_SESSION['uid'] = $credential->id; // simpan user ke database if (!$this->modelUser->cekUser($credential->id)) { // simpan user baru $properties = array(); $properties['oauth_uid'] = $credential->id; $properties['oauth_token'] = $access_token['oauth_token']; $properties['oauth_secret'] = $access_token['oauth_token_secret']; $properties['screen_name'] = $credential->screen_name; $this->modelUser->addUser($properties); } else { // update tanggal login $this->modelUser->logIn($credential->id); } // redirect to homepage header('Location: ' . httpRootPath); return true; }
public static function doBotLogin() { $oauth_token = HTMLResponse::fromGETorPOST('oauth_token'); $oauth_verifier = HTMLResponse::fromGETorPOST('oauth_verifier'); if ($oauth_token && $oauth_verifier && $oauth_token === $_SESSION['oauth_token_bot']) { $connection = new TwitterOAuth(CONSUMER_KEY_BOT, CONSUMER_SECRET_BOT, $oauth_token, $_SESSION['oauth_token_secret_bot']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier)); if ($userid = $access_token['user_id']) { $botConfig['twitter-userid'] = $userid; $botConfig['twitter-username'] = $access_token['screen_name']; $botConfig['oauth_token'] = $access_token['oauth_token']; $botConfig['oauth_token_secret'] = $access_token['oauth_token_secret']; $botConfig['dateline'] = time(); unset($_SESSION['oauth_token_bot']); $con = "<?php return " . var_export($botConfig, true) . "; ?>"; file_put_contents("botconfig.php", $con); return true; } } return false; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->getSettings(); $active = $config->get('active'); $consumer_key = $config->get('consumer_key'); $consumer_secret = $config->get('consumer_secret'); if ($active && $consumer_key && $consumer_secret) { $connection = new TwitterOAuth($consumer_key, $consumer_secret); $request_token = $connection->oauth('oauth/request_token'); $form['check'] = ['#type' => 'fieldset', '#title' => t('OAuth Check'), '#description' => t('Requested Token and Token Secret')]; $form['check']['token'] = ['#type' => 'textfield', '#title' => t('OAuth Token'), '#default_value' => $request_token['oauth_token']]; $form['check']['secret'] = ['#type' => 'textfield', '#title' => t('OAuth Token Secret'), '#default_value' => $request_token['oauth_token_secret']]; $form['check']['authorize'] = ['#type' => 'markup', '#title' => t('Authorize'), '#markup' => '<a href="' . $this->url('ibloggedabout.twitter', ['op' => 'authorize']) . '">Authorize</a>']; } // Module settings $form['active'] = ['#type' => 'checkbox', '#title' => t('Activate automated Twitter tweets.'), '#default_value' => $active]; // Twitter/Oauth settings $form['oauth'] = ['#type' => 'fieldset', '#title' => t('OAuth Settings'), '#description' => t('To enable OAuth based access for Twitter, you must <a href="@url">register your application</a> with Twitter and add the provided keys here.', ['@url' => 'https://dev.twitter.com/apps/new'])]; $form['oauth']['consumer_key'] = ['#type' => 'textfield', '#title' => t('OAuth Consumer key'), '#default_value' => $consumer_key]; $form['oauth']['consumer_secret'] = ['#type' => 'textfield', '#title' => t('OAuth Consumer secret'), '#default_value' => $consumer_secret]; return parent::buildForm($form, $form_state); }
public function verifyuser() { if (isset($_REQUEST['oauth_verifier'])) { $request_token = []; $request_token['oauth_token'] = $_SESSION['oauth_token']; $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; $user = array(); if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) { // Abort! Something is wrong. $user['error'] = 'tokens do not match'; } else { //Alright $connection = new TwitterOAuth($this->twitter_app_id, $this->twitter_api_secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $connection->setTimeouts(10, 15); //To prevent timeout error $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); $_SESSION['access_token'] = $access_token; $user = $access_token; } return $user; } }
function redirectToTwitter() { if (isset($_GET['token_id'])) { $_SESSION['TATAG_TOKEN_ID'] = $_GET['token_id']; } if (isset($_GET['otk'])) { $_SESSION['TATAG_OTK'] = $_GET['otk']; } if (isset($_GET['next'])) { $_SESSION['TATAG_NEXT'] = $_GET['next']; } $oauth = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET); $token = $oauth->oauth("oauth/request_token", array("oauth_callback" => HOME . '/login_tw.php')); if (!$token or !$token['oauth_token']) { Error::http(500, json_encode($requestToken)); } // Saving them into the session $_SESSION['oauth_token'] = $token['oauth_token']; $_SESSION['oauth_token_secret'] = $token['oauth_token_secret']; // Let's generate the URL and redirect $url = $oauth->url('oauth/authorize', array('oauth_token' => $token['oauth_token'])); //print_r($_SESSION); exit(); header('Location: ' . $url); }
<?php session_start(); require_once 'common.php'; require_once 'twitteroauth-0.4.1/autoloader.php'; use Abraham\TwitterOAuth\TwitterOAuth; //TwitterOAuth をインスタンス化 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); //コールバックURLをここでセット $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); //callback.phpで使うのでセッションに入れる $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; //Twitter.com 上の認証画面のURLを取得 $url = $connection->url('oauth/authenticate', array('oauth_token' => $request_token['oauth_token'])); //Twitter.com の認証画面へリダイレクト header('location: ' . $url);
public function index() { $user_data = ""; // if(Session::has('login_type')){ // $_SESSION['login_type'] = Session::get('login_type'); // } if (isset($_SESSION['login_type'])) { if ($_SESSION['login_type'] == "facebook") { $fb = new Facebook\Facebook(['app_id' => Config::get('facebook.appid'), 'app_secret' => Config::get('facebook.secret'), 'default_graph_version' => 'v2.2']); $helper = $fb->getRedirectLoginHelper(); if (!isset($_SESSION['fb_access_token'])) { try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (!isset($accessToken)) { if ($helper->getError()) { header('HTTP/1.0 401 Unauthorized'); echo "Error: " . $helper->getError() . "\n"; echo "Error Code: " . $helper->getErrorCode() . "\n"; echo "Error Reason: " . $helper->getErrorReason() . "\n"; echo "Error Description: " . $helper->getErrorDescription() . "\n"; } else { header('HTTP/1.0 400 Bad Request'); echo 'Bad request'; } exit; } $oAuth2Client = $fb->getOAuth2Client(); $tokenMetadata = $oAuth2Client->debugToken($accessToken); $tokenMetadata->validateAppId(Config::get('facebook.appid')); $tokenMetadata->validateExpiration(); if (!$accessToken->isLongLived()) { // Exchanges a short-lived access token for a long-lived one try { $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n"; exit; } } $_SESSION['fb_access_token'] = (string) $accessToken; } try { // Returns a `Facebook\FacebookResponse` object $response = $fb->get('/me?fields=id,name,last_name', $_SESSION['fb_access_token']); } catch (Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $user = $response->getGraphNode(); $user_data = array('type' => $_SESSION['login_type'], 'Name' => $user->getField('name'), 'ID' => $user->getField('id')); } else { if ($_SESSION['login_type'] == "google") { $user_data = array('type' => $_SESSION['login_type'], 'Name' => $_SESSION['name'], 'ID' => $_SESSION['id']); } else { if (isset($_REQUEST['oauth_verifier'])) { if (!isset($_SESSION['access_token'])) { $request_token = []; $request_token['oauth_token'] = $_SESSION['oauth_token']; $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; $connection = new TwitterOAuth(Config::get('twitter.appid'), Config::get('twitter.secret'), $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); $_SESSION['access_token'] = $access_token; } } $access_token = $_SESSION['access_token']; $connection = new TwitterOAuth(Config::get('twitter.appid'), Config::get('twitter.secret'), $access_token['oauth_token'], $access_token['oauth_token_secret']); $user = $connection->get("account/verify_credentials"); $user_data = array('type' => $_SESSION['login_type'], 'Name' => $user->screen_name, 'ID' => $user->id); } } $user = App\user::where('oauthID', $user_data['ID'])->first(); if (is_null($user)) { return view('Registration.registration', $user_data); } $_SESSION['user_id'] = $user_data['ID']; return view('home', $user_data); } else { return redirect("/"); } }
<?php // Authorization ini_set('display_errors', 1); // require_once('TwitterAPIExchange.php'); require 'TwitterOAuth/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; define('CONSUMER_KEY', 'HDhjz43hHgbl6B7fEVy3wHApk'); define('CONSUMER_SECRET', '9xaTyEdOWSs8O9JCdHUjnYpZCoTj1pn75y7FmAS4o8EzH83LPu'); define('OAUTH_CALLBACK', 'http://twitterfeed.web.engr.illinois.edu/TweedStudy/index.php'); if (!isset($_SESSION['oauth_access_token']) || $_SESSION['oauth_access_token'] == '') { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']); // echo $_REQUEST['oauth_verifier']; // echo "<br>"; $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); $_SESSION['oauth_access_token'] = $access_token['oauth_token']; $_SESSION['oauth_access_token_secret'] = $access_token['oauth_token_secret']; } else { // echo $_SESSION['oauth_access_token']; // echo "<br>"; } // $request_token = []; // $request_token['oauth_token'] = $_SESSION['oauth_token']; // $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret']; $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']); // Create $user variable from connection and store $user["id"] as session variable $user = $connection->get("account/verify_credentials"); $user_id = json_decode(json_encode($user), true); $user_id = $user_id["id"]; $_SESSION["user_id"] = $user_id; // Session variable that hold $user_id
/** * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException * @expectedExceptionMessage Invalid request token * @depends testOauthRequestToken */ public function testOauthAccessTokenTokenException(array $requestToken) { // Can't test this without a browser logging into Twitter so check for the correct error instead. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $requestToken['oauth_token'], $requestToken['oauth_token_secret']); $twitter->oauth("oauth/access_token", array("oauth_verifier" => "fake_oauth_verifier")); }