本文整理汇总了PHP中Facebook\Facebook::getOAuth2Client方法的典型用法代码示例。如果您正苦于以下问题:PHP Facebook::getOAuth2Client方法的具体用法?PHP Facebook::getOAuth2Client怎么用?PHP Facebook::getOAuth2Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Facebook\Facebook
的用法示例。
在下文中一共展示了Facebook::getOAuth2Client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loginFinish
/**
* {@inheritdoc}
*/
function loginFinish()
{
$helper = $this->api->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
throw new Hybrid_Exception('Facebook Graph returned an error: ' . $e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
throw new Hybrid_Exception('Facebook SDK returned an error: ' . $e->getMessage());
}
if (!isset($accessToken)) {
if ($helper->getError()) {
throw new Hybrid_Exception(sprintf("Could not authorize user, reason: %s (%d)", $helper->getErrorDescription(), $helper->getErrorCode()));
} else {
throw new Hybrid_Exception("Could not authorize user. Bad request");
}
}
try {
// Validate token
$oAuth2Client = $this->api->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$tokenMetadata->validateAppId($this->config["keys"]["id"]);
$tokenMetadata->validateExpiration();
// Exchanges a short-lived access token for a long-lived one
if (!$accessToken->isLongLived()) {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
}
} catch (FacebookSDKException $e) {
throw new Hybrid_Exception($e->getMessage(), 0, $e);
}
$this->setUserConnected();
$this->token("access_token", $accessToken->getValue());
}
示例2: getMe
/**
* Return info about facebook user
* @param $fields
* @return array
* @throws Exception
*/
public function getMe($fields)
{
$client = $this->fb->getOAuth2Client();
$accessTokenObject = $this->helper->getAccessToken();
if ($accessTokenObject == null) {
throw new Exception("User not allowed permissions");
}
if ($fields == "" || !is_array($fields) || count($fields) == 0) {
//array is empty
$fields = array(ID);
//set ID field
}
try {
$accessToken = $client->getLongLivedAccessToken($accessTokenObject->getValue());
$response = $this->fb->get("/me?fields=" . implode(",", $fields), $accessToken);
$this->setSocialLoginCookie(self::SOCIAL_NAME);
return $response->getDecodedBody();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
throw new Exception($e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
throw new Exception($e->getMessage());
}
}
示例3: getTokenInfo
/**
* @param string $token
*
* @return UserProfileInterface|null
*/
protected function getTokenInfo($token)
{
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
$response = $this->facebook->get('/me?fields=id,name,email,first_name,last_name', $token);
$user = $response->getGraphUser();
// check if we can get user identifier
if (empty($user->getId())) {
return null;
}
// do not accept tokens generated not for our application even if they are valid,
// to protect against "man in the middle" attack
$tokenMetadata = $this->facebook->getOAuth2Client()->debugToken($token);
// this is not required, but lets be sure because facebook API changes very often
$tokenMetadata->validateAppId($this->facebook->getApp()->getId());
$userProfile = new UserProfile();
$userProfile->setIdentifier($user->getId());
$userProfile->setDisplayName($user->getName());
$userProfile->setFirstName($user->getFirstName());
$userProfile->setLastName($user->getLastName());
$userProfile->setEmail($user->getEmail());
// facebook doesn't allow login with not verified email
if (!empty($user->getEmail())) {
$userProfile->setEmailVerified(true);
}
return $userProfile;
} catch (FacebookSDKException $e) {
return null;
}
}
示例4: getLongLivedAccessToken
/**
* @param string $userAccessToken
* @return \Facebook\Authentication\AccessToken|null
* @throws LiveBroadcastOutputException
*/
public function getLongLivedAccessToken($userAccessToken)
{
if (!$this->facebookSDK) {
$this->initFacebook();
}
if (!$userAccessToken) {
return null;
}
try {
return $this->facebookSDK->getOAuth2Client()->getLongLivedAccessToken($userAccessToken);
} catch (FacebookSDKException $ex) {
throw new LiveBroadcastOutputException('Facebook SDK exception: ' . $ex->getMessage());
}
}
示例5: _getUsername
/**
* Get email address or name based on access token.
*
* @param AccessToken $accessToken
*
* @return mixed
*/
protected function _getUsername($accessToken)
{
$oAuth2Client = $this->_facebook->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$user = $this->_facebook->get('/' . $tokenMetadata->getUserId() . '?fields=name,email', $accessToken)->getGraphUser();
return $user->getField('email') !== null ? $user->getField('email') : $user->getField('name');
}
示例6: indexAction
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$this->get('session')->start();
$config = ['app_id' => '1690300947880602', 'app_secret' => '0b6107ea187ee9cd79adc66a2dd84254', 'default_graph_version' => 'v2.5', 'persistent_data_handler' => 'session'];
$fb = new Facebook\Facebook($config);
$helper = $fb->getRedirectLoginHelper();
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;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($config['app_id']);
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$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;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
return new Response();
}
示例7: getMe
/**
* Return info about login user
* @return array
* @throws Exception
*/
public function getMe()
{
$client = $this->fb->getOAuth2Client();
$accessTokenObject = $this->helper->getAccessToken();
if ($accessTokenObject == null) {
throw new Exception("User not allowed permissions");
}
try {
$accessToken = $client->getLongLivedAccessToken($accessTokenObject->getValue());
$response = $this->fb->get('/me?fields=id,name,email', $accessToken);
return $response->getDecodedBody();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
throw new Exception($e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
throw new Exception($e->getMessage());
}
}
示例8: getTokenFromRedirect
/**
* This implementation follow instruction
*
* @see https://developers.facebook.com/docs/php/gettingstarted/5.0.0
*
* @return AccessToken
*/
public function getTokenFromRedirect()
{
$token = null;
try {
$token = $this->getLoginHelper()->getAccessToken($this->getRedirectUrl());
// exchange a long-live access token
$oAuth2Client = $this->facebook->getOAuth2Client();
$token = $oAuth2Client->getLongLivedAccessToken($token);
} catch (FacebookResponseException $e) {
throw new \RuntimeException('Graph returned an error: ' . $e->getMessage());
} catch (FacebookSDKException $e) {
// When validation fails or other local issues
throw new \RuntimeException('Facebook SDK returned an error: ' . $e->getMessage());
}
if (!$token instanceof AccessToken) {
throw new \RuntimeException('Could not authorize request on Facebook');
}
// store access token
$_SESSION[self::FACEBOOK_SESSION_KEY] = $token->__toString();
$this->facebook->setDefaultAccessToken($token);
return $token;
}
示例9: getUserAccessToken
/**
* Get user access token.
*
* @param bool $longLived Exchange short-lived access token for a long-lived one
* @return AccessToken
* @throws SystemException
*/
public function getUserAccessToken($longLived = false)
{
$accessToken = null;
$helper = $this->fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (FacebookResponseException $e) {
Log::error('Facebook Graph API call failed when getting an access token.', ['error' => $e->getMessage(), 'code' => $e->getCode(), 'subcode' => $e->getSubErrorCode(), 'status' => $e->getHttpStatusCode(), 'type' => $e->getErrorType()]);
throw new SystemException('Facebook Graph API error: ' . $e->getMessage());
} catch (FacebookSDKException $e) {
Log::error('Facebook SDK error when getting an access token.', ['error' => $e->getMessage(), 'code' => $e->getCode()]);
throw new SystemException('Facebook SDK error: ' . $e->getMessage());
}
if ($longLived && $accessToken && !$accessToken->isLongLived()) {
try {
$oAuth2Client = $this->fb->getOAuth2Client();
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (FacebookSDKException $e) {
Log::error('Facebook SDK error when getting a long-lived access token.', ['error' => $e->getMessage(), 'code' => $e->getCode()]);
}
}
return $accessToken;
}
示例10: getClient
public function getClient()
{
return $this->application->getOAuth2Client();
}
示例11: init
public function init()
{
parent::init();
$fb_client = new Facebook(['app_id' => $this->appId, 'app_secret' => $this->secret, 'default_graph_version' => 'v2.2']);
$fb_helper = $fb_client->getRedirectLoginHelper();
try {
$accessToken = $fb_helper->getAccessToken();
} catch (FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
if ($fb_helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $fb_helper->getError() . "\n";
echo "Error Code: " . $fb_helper->getErrorCode() . "\n";
echo "Error Reason: " . $fb_helper->getErrorReason() . "\n";
echo "Error Description: " . $fb_helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
$oAuth2Client = $fb_client->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
$tokenMetadata->validateAppId($this->appId);
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $fb_helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb_client->get('/me?fields=id,name', $accessToken);
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
}
示例12: index
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("/");
}
}
示例13: Facebook
<?php
use Facebook\Facebook;
use API\src\Config\Config;
use API\src\Server\Session;
require_once __DIR__ . '/../Autoload.php';
$fb = new Facebook(['app_id' => Config::getConfig('FacebookAppId'), 'app_secret' => Config::getConfig('FacebookSecret'), 'default_graph_version' => Config::getConfig('FacebookAPIVersion')]);
$app = $fb->getApp();
$accessToken = $app->getAccessToken();
$accessTokenValue = $accessToken->getValue();
$oauth2 = $fb->getOAuth2Client();
$tokenMeta = $oauth2->debugToken($accessToken);
$tokenMeta->validateAppId(Config::getConfig('FacebookAppId'));
$tokenMeta->validateExpiration();
示例14: fallbackAction
public function fallbackAction()
{
$fb = new Facebook(['app_id' => $this->getParameter('tanna_user.facebook.app_id'), 'app_secret' => $this->getParameter('tanna_user.facebook.app_secret'), 'default_graph_version' => 'v2.2']);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (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;
}
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId((string) $this->getParameter('tanna_user.facebook.app_id'));
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
}
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,email,first_name,last_name,gender,locale,timezone,location,birthday', $accessToken);
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$facebookUser = $response->getGraphUser();
$om = $this->get('tanna_user.doctrine.om');
$user = $om->getRepository($this->getParameter('tanna_user.user.class'))->findOneByEmail($facebookUser['email']);
//register user
if ($user === null) {
$response = $this->registerUser($facebookUser);
if ($response instanceof UserInterface) {
//login user and redirect him
return $this->loginAndRedirect($response);
} else {
//@todo redirect and show errors
die('show errors');
}
} else {
return $this->loginAndRedirect($user);
}
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');
}
示例15: facebookLogin
public function facebookLogin()
{
// Small hack required for CakePHP 3.x and Facebook class
if (!session_id()) {
$this->request->session()->start();
}
$fb = new Facebook\Facebook(['app_id' => FB_APP_ID, 'app_secret' => FB_SECRET, 'default_graph_version' => FB_API_VERSION]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
$this->log('Graph returned an error: ' . $e->getMessage());
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$this->log('Facebook SDK returned an error: ' . $e->getMessage());
exit;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
$this->log("Error: " . $helper->getError());
$this->log("Error Code: " . $helper->getErrorCode());
$this->log("Error Reason: " . $helper->getErrorReason());
$this->log("Error Description: " . $helper->getErrorDescription());
} else {
header('HTTP/1.0 400 Bad Request');
$this->log('Bad request');
}
exit;
}
// Logged in
$this->log($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId(FB_APP_ID);
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$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;
}
}
$response = $fb->get('/me?fields=id,name,email', (string) $accessToken);
$facebookUser = $response->getGraphUser();
if ($this->validateUserExists($facebookUser['email'])) {
$user = $this->Users->getByEmail($facebookUser['email']);
} else {
$userData = array('name' => $facebookUser['name'], 'phone' => null, 'email' => $facebookUser['email'], 'password' => '', 'modified' => new \DateTime('now'), 'created' => new \DateTime('now'), 'profile' => PROFILE_CUSTOMER, 'facebook_id' => $facebookUser['id']);
$user = $this->Users->newEntity($userData);
$user = $this->Users->save($user);
}
if ($user) {
$this->Auth->setUser($user->toArray());
}
$this->redirect('/dashboard');
}