/** * If not authorized, build the URL to authorize the app * * @return string * @throws \Abraham\TwitterOAuth\TwitterOAuthException */ public function buildTwitterAuthorizeUrl() { $initialConnection = new \Abraham\TwitterOAuth\TwitterOAuth($this->config['CONSUMER_KEY'], $this->config['CONSUMER_SECRET']); $request_token = $initialConnection->oauth('oauth/request_token', array('oauth_callback' => $this->config['OAUTH_CALLBACK'])); $this->session['twitter_oauth_token'] = $request_token['oauth_token']; $this->session['twitter_oauth_token_secret'] = $request_token['oauth_token_secret']; return $initialConnection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); }
/** * @param string $returnUrl * @return string */ public function getAuthorizationUri($returnUrl = NULL) { $connection = new \Abraham\TwitterOAuth\TwitterOAuth($this->app_key, $this->app_secret); $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => $this->redirect_uri)); $this->session->putData('return_url', $returnUrl); $this->session->putData('oauth_token', $request_token['oauth_token']); $this->session->putData('oauth_token_secret', $request_token['oauth_token_secret']); $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); return $url; }
<?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.'); }
$photo = $request->request->get('photo'); $helper = new \Helper\Helper(); $name = $helper->convertBase64ToTmpFile($photo); $url = $request->getScheme() . '://' . $request->getHost() . '/share/' . $name; $response = new \Symfony\Component\HttpFoundation\JsonResponse(['url' => $url]); $response->send(); }); $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'];
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); }
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; }
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) { $connection = new Abraham\TwitterOAuth\TwitterOAuth(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), session('access_token.oauth_token'), session('access_token.oauth_token_secret')); $statues = $connection->post("statuses/update", ["status" => $request->input('status')]);