/** * @param array $params * @throws Exception */ public function authenticate(array $params) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $oAuthConf = Config::$a['oauth']['providers'][$this->authProvider]; $client = new \OAuth2\Client($oAuthConf['clientId'], $oAuthConf['clientSecret'], \OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setAccessTokenType(\OAuth2\Client::ACCESS_TOKEN_BEARER); $response = $client->getAccessToken('https://ssl.reddit.com/api/v1/access_token', 'authorization_code', array('redirect_uri' => sprintf(Config::$a['oauth']['callback'], $this->authProvider), 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); // Reddit requires a User-Agent $info = $client->fetch("https://oauth.reddit.com/api/v1/me.json", array(), 'GET', array('User-Agent' => 'destiny.gg/' . Config::version())); if (empty($info['result']) || !is_array($info['result']) || isset($info['error'])) { throw new Exception('Invalid user details response'); } $authCreds = $this->getAuthCredentials($params['code'], $info['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
/** * @param array $params * @throws Exception */ public function authenticate(array $params, ViewModel $model) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $oAuthConf = Config::$a['oauth']['providers'][$this->authProvider]; $client = new \OAuth2\Client($oAuthConf['clientId'], $oAuthConf['clientSecret']); $client->setAccessTokenType(\OAuth2\Client::ACCESS_TOKEN_OAUTH); $response = $client->getAccessToken('https://api.twitch.tv/kraken/oauth2/token', 'authorization_code', array('redirect_uri' => sprintf(Config::$a['oauth']['callback'], $this->authProvider), 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); $response = $client->fetch('https://api.twitch.tv/kraken/user'); if (empty($response['result']) || isset($response['error'])) { throw new Exception('Invalid user details response'); } if (is_string($response['result'])) { throw new Exception(sprintf('Invalid auth result %s', $response['result'])); } $authCreds = $this->getAuthCredentials($params['code'], $response['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
public function callbackAction() { $callback_url = Mage::getUrl('gomage_social/reddit/callback', array('_secure' => true)); $client = new OAuth2\Client(Mage::getStoreConfig('gomage_social/reddit/id'), Mage::getStoreConfig('gomage_social/reddit/secret'), OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); $params = array("code" => $this->getRequest()->getParam('code'), "redirect_uri" => $callback_url); if ($params['code']) { $response = $client->getAccessToken($this->accessTokenUrl, "authorization_code", $params); $accessTokenResult = $response["result"]; $client->setAccessToken($accessTokenResult["access_token"]); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER); $response = (object) $client->fetch("https://oauth.reddit.com/api/v1/me.json"); $profile = null; switch ($response->code) { case 200: $profile = (object) $response->result; break; default: $this->getSession()->addError($this->__('Could not connect to Reddit. Refresh the page or try again later.')); return $this->_redirectUrl(); } if ($profile) { if ($profile->id) { $social_collection = Mage::getModel('gomage_social/entity')->getCollection()->addFieldToFilter('social_id', $profile->id)->addFieldToFilter('type_id', GoMage_Social_Model_Type::REDDIT); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $social_collection->addFieldToFilter('website_id', Mage::app()->getWebsite()->getId()); } $social = $social_collection->getFirstItem(); if ($social && $social->getId()) { if ($social->social_id == $profile->id) { $customer = Mage::getModel('customer/customer'); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); } $customer->load($social->getData('customer_id')); if ($customer && $customer->getId()) { if (!$customer->getConfirmation()) { $this->getSession()->loginById($customer->getId()); } else { $this->getSession()->addError($this->__('This account is not confirmed.')); } } } } else { $profile->url = Mage::getUrl('gomage_social/reddit/checkEmail', array('_secure' => true)); $profile->urlEmailClose = Mage::getUrl('gomage_social/twitter/emailClose', array('_secure' => true)); $profile->type_id = GoMage_Social_Model_Type::REDDIT; Mage::getSingleton('core/session')->setGsProfile($profile); } } } } return $this->_redirectUrl(); }
/** * @param array $params * @throws Exception */ public function authenticate(array $params) { if (!isset($params['code']) || empty($params['code'])) { throw new Exception('Authentication failed, invalid or empty code.'); } $authConf = Config::$a['oauth']['providers'][$this->authProvider]; $callback = sprintf(Config::$a['oauth']['callback'], $this->authProvider); $client = new \OAuth2\Client($authConf['clientId'], $authConf['clientSecret']); $response = $client->getAccessToken('https://accounts.google.com/o/oauth2/token', 'authorization_code', array('redirect_uri' => $callback, 'code' => $params['code'])); if (empty($response) || isset($response['error'])) { throw new Exception('Invalid access_token response'); } if (!isset($response['result']) || empty($response['result']) || !isset($response['result']['access_token'])) { throw new Exception('Failed request for access token'); } $client->setAccessToken($response['result']['access_token']); $response = $client->fetch('https://www.googleapis.com/oauth2/v2/userinfo'); if (empty($response['result']) || isset($response['error'])) { throw new Exception('Invalid user details response'); } $authCreds = $this->getAuthCredentials($params['code'], $response['result']); $authCredHandler = new AuthenticationRedirectionFilter(); return $authCredHandler->execute($authCreds); }
die; //?state=SomeUnguessableValue&code=ki4tr-_EAXNXVrhWMcQ5a5pLm0o } $authorizeUrl = 'https://ssl.reddit.com/api/v1/authorize'; $accessTokenUrl = 'https://ssl.reddit.com/api/v1/access_token'; $clientId = 'jJgLD5ebMOT9sw'; $clientSecret = 'muldwiysWI2ok2KWNmoiDK6FMKw'; $userAgent = 'ChangeMeClient/0.1 by YourUsername'; $redirectUrl = "http://brittanyannkos.com/reddit"; require "OAuth2/Client.php"; require "OAuth2/GrantType/IGrantType.php"; require "OAuth2/GrantType/AuthorizationCode.php"; $client = new OAuth2\Client($clientId, $clientSecret, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); $_GET["code"] = "ki4tr-_EAXNXVrhWMcQ5a5pLm0o"; if (!isset($_GET["code"])) { $authUrl = $client->getAuthenticationUrl($authorizeUrl, $redirectUrl, array("scope" => "identity", "state" => "SomeUnguessableValue", "duration" => "permanent")); header("Location: " . $authUrl); die("Redirect"); } else { $params = array("code" => $_GET["code"], "redirect_uri" => $redirectUrl); $response = $client->getAccessToken($accessTokenUrl, "authorization_code", $params); $accessTokenResult = $response["result"]; print_r($accessTokenResult); $client->setAccessToken($accessTokenResult["access_token"]); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER); $response = $client->fetch("https://oauth.reddit.com/api/v1/me.json"); echo '<strong>Response for fetch me.json:</strong><pre>'; print_r($response); echo '</pre>'; }
require 'IGrantType.php'; require 'AuthorizationCode.php'; /******************************************************************************* * IDENTIFICATION ******************************************************************************/ $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET); $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $url = $protocol . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI']; //DECONNEXION if (isset($_GET['reset'])) { unset($_SESSION['access_token']); header('Content-type: application/json'); echo json_encode(array(DISCONNECT_PAGE)); } elseif (!empty($_SESSION['access_token'])) { $client->setAccessToken($_SESSION['access_token']); $profile = $client->fetch(PROFILE_PAGE); } elseif (empty($_GET['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, $url); header('Location: ' . $auth_url); die('Redirect'); } else { $params = array('code' => $_GET['code'], 'redirect_uri' => $url); $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); if (isset($response['result']) && isset($response['result']['access_token'])) { $client->setAccessToken($response['result']['access_token']); $_SESSION['access_token'] = $response['result']['access_token']; $client->setAccessToken($_SESSION['access_token']); $profile = $client->fetch(PROFILE_PAGE); } else { die('Une erreur est survenue lors de l’identification'); }
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); if (!isset($_REQUEST['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); header('Location: ' . $auth_url); die('Redirecting ...'); } else { $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params); $val = (object) $response['result']; if (!$val->access_token) { throw new Exception("No access token returned: " . $val->error_description); } $client->setAccessToken($val->access_token); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH); // Submit a file first $response = $client->fetch(SUBMIT_API, array('title' => 'Fella Sample Image', 'artist_comments' => 'Fella Sample Image', 'keywords' => 'fella sample image', 'folder' => APPNAME, 'file' => "@fella.png"), OAuth2\Client::HTTP_METHOD_POST); $result = (object) $response['result']; if (!$result) { throw new Exception('No valid JSON response returned'); } if ($result->status != 'success') { throw new Exception($result->error_description); } // Rename the folder we just created $new_folder = NEW_FOLDER_NAME . uniqid(); $target_folderid = $result->folderid; $response = $client->fetch(FOLDER_API, array('folderid' => $target_folderid, 'folder' => $new_folder), OAuth2\Client::HTTP_METHOD_POST); $result = (object) $response['result']; if (!$result) { throw new Exception('No valid JSON response returned'); }
require 'GrantType/IGrantType.php'; require 'GrantType/AuthorizationCode.php'; const CLIENT_ID = '379875764619-57lesbrrec02g9tf3r2d2gup6nvdl3p4.apps.googleusercontent.com'; const CLIENT_SECRET = 'KeHot5Ds76apeUcrE27xXYJH'; const REDIRECT_URI = 'http://localhost/oauth/test.php'; const AUTHORIZATION_ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'; const TOKEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token'; //localhost/oauth/test.php session_start(); $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET); if (!isset($_GET['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI, array('scope' => 'http://www.google.com/m8/feeds/', 'access_type' => 'offline')); header('Location: ' . $auth_url); die('Redirect'); } else { if (empty($_SESSION['token'])) { $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); if ($response['code'] == 200) { $_SESSION['token'] = $response['result']['access_token']; } } if (!empty($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); $response = $client->fetch('https://www.google.com/m8/feeds/contacts/default/full'); if ($response['code'] == 200) { $response = json_encode(simplexml_load_string($response['result'])); print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; } } }
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); if (!isset($_REQUEST['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); header('Location: ' . $auth_url); die('Redirecting ...'); } else { $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params); $val = (object) $response['result']; if (!$val->access_token) { throw new Exception("No access token returned: " . $val->error_description); } $client->setAccessToken($val->access_token); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH); // Submit a file first $response = $client->fetch(SUBMIT_API, array('title' => 'Fella Sample Image', 'artist_comments' => 'Fella Sample Image', 'keywords' => 'fella sample image', 'folder' => APPNAME, 'file' => "@fella.png"), OAuth2\Client::HTTP_METHOD_POST); $result = (object) $response['result']; if (!$result) { throw new Exception('No valid JSON response returned'); } if ($result->status != 'success') { throw new Exception($result->error_description); } $target_folderid = $result->folderid; $move_folder = 'My folder ' . uniqid(); // Move submission in new folder $response = $client->fetch(MOVE_API, array('stashid' => $result->stashid, 'folder' => $move_folder), OAuth2\Client::HTTP_METHOD_POST); $result = (object) $response['result']; if (!$result) { throw new Exception('No valid JSON response returned'); }
// authentication call $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); header('Location: ' . $auth_url); die('Redirect'); } else { // access token request $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); // CAUTION! instead of parse_str, assign response directly to $info or find an alternative way. // parse_str($response['result'], $info); $info = $response['result']; $accessToken = $info['access_token']; $refreshToken = $info['refresh_token']; print_r($info); echo "<br /><br /><br />"; // set access token for further api calls $client->setAccessToken($accessToken); // example api call $response = $client->fetch('https://api.parasut.com/v1/me'); var_dump($response, $response['result']); echo "<br /><br /><br />"; // in the case of token expiration, use method below $params = array('refresh_token' => $refreshToken); $response = $client->getAccessToken(TOKEN_ENDPOINT, 'refresh_token', $params); $info = $response['result']; print_r($info); $client->setAccessToken($info['access_token']); $response = $client->fetch('https://api.parasut.com/v1/33/sales_invoices'); var_dump($response, $response['result']); } die;
/** * Add the necessary webhooks for the Sausage Machine to function * @param String $github_access_token see route_get_github_auth & route_get_github_auth_callback * @param String $github_repo GitHub username, followed by a slash, followed by the name of the respository * @return true if sucessful, false if not */ function github_add_webhook($github_access_token, $github_repo) { $client = new OAuth2\Client(config('github_client_id'), config('github_client_secret')); $client->setAccessToken($github_access_token); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_TOKEN); $client->setCurlOption(CURLOPT_USERAGENT, config('github_useragent')); $param = array('name' => 'web', 'active' => true, 'events' => array('push'), 'config' => array('url' => base_url() . 'github.php?push', 'content_type' => 'form')); $response = $client->fetch('https://api.github.com/repos/' . $github_repo . '/hooks', json_encode($param), 'POST'); if (!isset($response['code']) || $response['code'] !== 201) { return false; } else { return true; } }
$client = new OAuth2\Client($clientId, $clientSecret, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC); $client->setCurlOption(CURLOPT_USERAGENT, $userAgent); if (!isset($_GET["code"])) { $authUrl = $client->getAuthenticationUrl($authorizeUrl, $redirectUrl, array("scope" => "user,user:email", "state" => "SomeUnguessableValue")); header("Location: " . $authUrl); die("Redirect"); } else { $params = array("code" => $_GET["code"], "redirect_uri" => $redirectUrl); $response = $client->getAccessToken($accessTokenUrl, "authorization_code", $params); $accessTokenResult = $response["result"]; parse_str($accessTokenResult, $accessTokenResultArray); $accessTokenFirst = $accessTokenResultArray["access_token"]; $accessToken = $accessTokenResultArray["access_token"]; $client->setAccessToken($accessToken); $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER); $response = $client->fetch("https://api.github.com/user"); $responseArray = $response["result"]; $login = $responseArray["login"]; $email = $responseArray["email"]; $_SESSION["user_name"] = $login; require_once 'mysql_connect.php'; // check if exist $queryExist = "SELECT user_name,last_page from users WHERE user_name = '{$login}'"; $responseExist = @mysqli_query($dbc, $queryExist); if (!$responseExist) { die('Query failed to execute for some reason'); } // get the nb of the actual page $actual_page_nb = 0; require_once 'mysql_connect.php'; $actual_page_url = $_SESSION['actual_url'];