/** * After authorize call back call this method with request parameters to get the Twitter access token * Once the token is recieved, it will be persisted in the session * * @param $oauth_token * @param $oauth_verifier * @return \Abraham\TwitterOAuth\TwitterOAuth * @throws \Abraham\TwitterOAuth\TwitterOAuthException */ public function setAccessToken($oauth_token, $oauth_verifier) { $initialConnection = new \Abraham\TwitterOAuth\TwitterOAuth($this->config['CONSUMER_KEY'], $this->config['CONSUMER_SECRET'], $this->session['twitter_oauth_token'], $this->session['twitter_oauth_token_secret']); $access_token = $initialConnection->oauth("oauth/access_token", ["oauth_token" => $oauth_token, "oauth_verifier" => $oauth_verifier]); $this->session['twitter_access_token'] = $access_token; return $this->setAuthenticatedConnection($access_token); }
/** * @param string $oauth_verifier * @return array */ public function getToken($oauth_verifier) { $connection = new \Abraham\TwitterOAuth\TwitterOAuth($this->app_key, $this->app_secret, $this->session->getData('oauth_token'), $this->session->getData('oauth_token_secret')); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier)); $connection = new \Abraham\TwitterOAuth\TwitterOAuth($this->app_key, $this->app_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); $credentials = $connection->get('/account/verify_credentials', ['include_entities' => true, 'skip_status' => true, 'include_email' => true]); $access_token['since_id'] = $credentials; return $access_token; }
public function callback() { $oauth_token = $this->session->userdata('twitter_oauth_token'); $oauth = new \Abraham\TwitterOAuth\TwitterOAuth(TWITTER_KEY, TWITTER_SECRET_KEY, $oauth_token, $_GET['oauth_token']); $access_token = $oauth->oauth("oauth/access_token", array("oauth_verifier" => $_GET['oauth_verifier'])); $user_account = array('user_id' => $this->user->id, 'account_id' => $access_token['user_id'], 'type' => 'twitter', 'access_token' => (string) json_encode($access_token)); $this->load->model('user_account_model'); if ($this->user_account_model->save($user_account)) { redirect(base_url() . "main/myaccount/twitter"); } }
<?php require "../bootstrap.php"; $twitteroauth = new Abraham\TwitterOAuth\TwitterOAuth('kcUhArVLnK6xzDpVJU85r7FJj', 'dekH6UMluN2ZCCJ0v1q95w3DaIMeczsP1l3wAEjRwbn16G43aV'); $request_token = $twitteroauth->oauth("oauth/request_token", array('oauth_callback' => 'http://api1.papangping.com/twitter/oauthcallback.php')); $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; // If everything goes well.. if ($request_token['oauth_token']) { // Let's generate the URL and redirect $url = $twitteroauth->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token'])); header('Location: ' . $url); } else { // It's a bad idea to kill the script, but we've got to know when there's an error. die('Something wrong happened.'); }
}); $app->post('/authTwitter', function () { session_start(); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $_SESSION['image'] = $request->request->get('photo'); $_SESSION['text'] = $request->request->get('text'); $connection = new \Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => TWITTER_OAUTH_ENDPOINT)); $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; $authURL = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); $response = new \Symfony\Component\HttpFoundation\JsonResponse(['url' => $authURL]); $response->send(); }); $app->get('/shareTwitter', function () { session_start(); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $_SESSION['oauth_token'] = $request->query->get('oauth_token'); $_SESSION['oauth_verifier'] = $request->query->get('oauth_verifier'); $connection = new \Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_SESSION['oauth_verifier'])); $_SESSION['oauth_token'] = $access_token['oauth_token']; $_SESSION['oauth_token_secret'] = $access_token['oauth_token_secret']; $_SESSION['twitter_user_id'] = $access_token['user_id']; $connection = new \Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $imageURL = $_SESSION['image']; $photo = $connection->upload('media/upload', ['media' => $imageURL]); $statues = $connection->post("statuses/update", ['status' => $_SESSION['text'], 'media_ids' => $photo->media_id]); $response = new \Symfony\Component\HttpFoundation\RedirectResponse('https://twitter.com'); $response->send(); }); $app->run();
public function loginbytwitter() { $connection = new Abraham\TwitterOAuth\TwitterOAuth('5Gd9l295ZAkm6TP9HbTApXYb6', 'y7rXUAv2aqszUsn1O4t3SCmDlcJnWUuXqpj3kmgs3c10W9QIKV', '908751325-nW0w5sfktt4yzXlJ6ZoZTU9INTCKqsA78WWw8lON', 'jc9VcvyFDpYl9lxFpw4s44wv2dMpWEntIMNhsHS7UpYhQ'); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => 'http://acquire.social/register/twittercallback')); $_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'])); redirect($url); }
* This example is made for myself, if I loose my code */ session_start(); require_once __DIR__ . '/../vendor/autoload.php'; //Edit the following config variables $consumer_key = 'consumer_key'; $consumer_secret = 'consumer_secret'; //callback url. In this example it is using current url $callback = "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if (isset($_SESSION['oauth_token'])) { $oauth_token = $_SESSION['oauth_token']; unset($_SESSION['oauth_token']); $connection = new Abraham\TwitterOAuth\TwitterOAuth($consumer_key, $consumer_secret); //necessary to get access token other wise u will not have permision to get user info $params = array("oauth_verifier" => $_GET['oauth_verifier'], 'oauth_token' => $_GET['oauth_token']); $access_token = $connection->oauth('oauth/access_token', $params); //now again create new instance using updated return oauth_token and oauth_token_secret because old one expired if u dont u this u will also get token expired error $connection = new Abraham\TwitterOAuth\TwitterOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); $content = $connection->get('account/verify_credentials'); //Printing the profile data print_r($content); } else { //this code will return your valid url which u can use in iframe src to popup or can directly view the page as its happening in this example $connection = new Abraham\TwitterOAuth\TwitterOAuth($consumer_key, $consumer_secret); $temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" => $callback)); $_SESSION['oauth_token'] = $temporary_credentials['oauth_token']; $_SESSION['oauth_token_secret'] = $temporary_credentials['oauth_token_secret']; $url = $connection->url('oauth/authenticate', array('oauth_token' => $temporary_credentials['oauth_token'])); // REDIRECTING TO THE URL header('Location: ' . $url); }
<?php require "../bootstrap.php"; $twitteroauth = new Abraham\TwitterOAuth\TwitterOAuth('kcUhArVLnK6xzDpVJU85r7FJj', 'dekH6UMluN2ZCCJ0v1q95w3DaIMeczsP1l3wAEjRwbn16G43aV', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $access_token = $twitteroauth->oauth("oauth/access_token", array("oauth_verifier" => $_GET["oauth_verifier"])); if ($access_token) { $_SESSION['access_token'] = $access_token; var_dump($access_token); } else { session_destroy(); } header("Location: index.php");
public function get_twitter_auth_url() { $connection = new \Abraham\TwitterOAuth\TwitterOAuth(TWITTER_KEY, TWITTER_SECRET_KEY); $request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://demo.merlinleads.net/twitter/callback")); $oauth_token = $request_token['oauth_token']; $token_secret = $request_token['oauth_token_secret']; setcookie("token_secret", " ", time() - 3600); setcookie("token_secret", $token_secret, time() + 60 * 10); setcookie("oauth_token", " ", time() - 3600); setcookie("oauth_token", $oauth_token, time() + 60 * 10); $this->session->set_userdata('twitter_oauth_token', $oauth_token); $url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token)); return $url; }
return "hello world"; }); Route::get('/twitter-login-callback', ['as' => 'twitter_signin_callback', function (Illuminate\Http\Request $request) { if (!$request->has('oauth_verifier')) { return "Access denied !"; } $oauthVerifier = $request->input('oauth_verifier'); $connection = new Abraham\TwitterOAuth\TwitterOAuth(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), session('oauth_token'), session('oauth_token_secret')); $accessToken = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauthVerifier)); session()->forget('twitter_account'); session(['access_token' => $accessToken]); return redirect('twitter-profile'); }]); Route::get('/login', ['as' => 'login_path', function (Illuminate\Http\Request $request) { $connection = new Abraham\TwitterOAuth\TwitterOAuth(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET')); $requestToken = $connection->oauth('oauth/request_token', array('oauth_callback' => route('twitter_signin_callback'))); session(['oauth_token' => $requestToken['oauth_token'], 'oauth_token_secret' => $requestToken['oauth_token_secret']]); $urlTwitter = $connection->url('oauth/authenticate', ['oauth_token' => session('oauth_token')]); return view('layouts.login', compact('urlTwitter')); }]); Route::get('/twitter-profile', function (Illuminate\Http\Request $request) { $accessToken = session('access_token'); if (!session()->has('twitter_account')) { $connection = new Abraham\TwitterOAuth\TwitterOAuth(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), $accessToken['oauth_token'], $accessToken['oauth_token_secret']); $twitterAccount = $connection->get("account/verify_credentials"); session(['twitter_account' => $twitterAccount]); } $twitterAccount = session('twitter_account'); return view('twitter.index', compact('twitterAccount')); }); Route::post('/twitter-status', ['as' => 'twitter_status_update', function (Illuminate\Http\Request $request) {