本文整理汇总了PHP中Facebook\FacebookSession::getAccessToken方法的典型用法代码示例。如果您正苦于以下问题:PHP FacebookSession::getAccessToken方法的具体用法?PHP FacebookSession::getAccessToken怎么用?PHP FacebookSession::getAccessToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Facebook\FacebookSession
的用法示例。
在下文中一共展示了FacebookSession::getAccessToken方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFacebookSession
private function getFacebookSession($user)
{
// first see if a session has already been created for this user. if it has return that.
if (self::hasCachedFacebookSession($user->fb_access_token)) {
return self::getCachedFacebookSession($user->fb_access_token);
}
try {
$fbSession = new FacebookSession($user->fb_access_token);
$token = $fbSession->getAccessToken();
// check that the token is still valid and hasn't expired. This checks with facebook and fails if user has removed app.
if (!$token->isValid()) {
// if the token is invalid don't return the session.
// null should be cached in cachedFacebookSessions so that this check doesn't have to be made again on this request.
$fbSession = null;
}
} catch (Exception $e) {
Log::error('Exception when trying to get facebook session.', array('exception' => $e));
$fbSession = null;
}
// store in cache
$this->cacheFacebookSession($user->fb_access_token, $fbSession);
return $fbSession;
}
示例2: FacebookRequest
if ($session) {
//echo '<pre>' . print_r($session,1);
try {
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName() . '<br>';
echo "Email: " . $user_profile->getProperty("email") . '<br>';
echo "First Name: " . $user_profile->getProperty("first_name") . '<br>';
echo "Last Name: " . $user_profile->getProperty("last_name") . '<br>';
echo '<pre>';
print_r($user_profile);
echo '</pre>';
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
$accessToken = $session->getAccessToken();
$longLivedAccessToken = $accessToken->extend();
echo 'Short Access Token: ' . $accessToken . '<br/><br/>Long Access Token: ' . $longLivedAccessToken;
$request = new FacebookRequest($session, 'GET', '/me/feed');
$response = $request->execute();
$graphObject = $response->getGraphObject();
echo '<pre>';
print_r($graphObject);
echo '</pre>';
} else {
// Login URL if session not found
echo '<a href="' . $helper->getLoginUrl(array('scope' => 'email,read_stream,user_posts')) . '">Login</a>';
}
} catch (FacebookRequestException $ex) {
// Exception
} catch (Exception $ex) {
示例3: service_access
public function service_access()
{
$CI =& get_instance();
$response = array();
$page = $CI->input->get('page');
$user = $CI->input->get('user');
$idSource = $CI->input->get('dataSource');
$idProject = $CI->input->get('project');
$authResponse = $CI->input->get('authResponse');
if (!$authResponse || !isset($authResponse['accessToken']) || !$authResponse['accessToken']) {
$response['error'] = array('code' => 'SA11', 'type' => 'MissingParamError', 'msg' => sprintf(lang('services_missing_param'), 'accessToken'));
} else {
if (!$idSource || !$idProject) {
$response['error'] = array('code' => 'SA12', 'type' => 'MissingParamError', 'msg' => sprintf(lang('services_missing_param'), 'ProjectSource'));
} else {
if ($idSource != $this->getId()) {
$response['error'] = array('code' => 'SA12', 'type' => 'SecurityError', 'msg' => lang('services_access_denied'));
} else {
$this->loadProperties();
FacebookSession::setDefaultApplication($this->getProperty('fb_insights_field_app_id'), $this->getProperty('fb_insights_field_app_secret'));
// This will use de user's access_token to create a long live one
$session = new FacebookSession($authResponse['accessToken']);
try {
// User logged in, get the AccessToken entity.
$accessToken = $session->getAccessToken();
// Exchange the short-lived token for a long-lived token.
$longLivedAccessToken = $accessToken->extend();
/*
// When using too much a long lived token
// Get a code from a long-lived access token
$code = AccessToken::getCodeFromAccessToken($longLivedAccessToken);
// Get a new long-lived access token from the code
$longLivedAccessToken = AccessToken::getAccessTokenFromCode($code);
*/
// This will use de user's access_token to create a long live one
$session = new FacebookSession($longLivedAccessToken);
$request = new FacebookRequest($session, 'GET', '/' . $page['id'] . '?fields=id,category,link,name,website,access_token,cover,about');
$pageFound = $request->execute()->getGraphObject()->asArray();
if ($pageFound && isset($pageFound['access_token']) && $pageFound['access_token']) {
$info = (new AccessToken($pageFound['access_token']))->getInfo();
if (!$info->getExpiresAt()) {
$properties = new ProjectProperties($idSource, $idProject);
$properties->set(array('added' => 1, 'id_page' => $pageFound['id'], 'category' => $pageFound['category'], 'link' => $pageFound['link'], 'name' => $pageFound['name'], 'website' => isset($pageFound['website']) ? $pageFound['website'] : null, 'access_token' => $pageFound['access_token']));
$properties->save();
// Now store the page's perpetual-lived token in the database
$response['data'] = array(array('id' => $pageFound['id'], 'name' => $pageFound['name'], 'category' => isset($pageFound['category']) ? $pageFound['category'] : null, 'about' => isset($pageFound['about']) ? $pageFound['about'] : $pageFound['category'], 'link' => isset($pageFound['link']) ? $pageFound['link'] : null, 'website' => isset($pageFound['website']) && strpos($pageFound['website'], '<') === false ? $pageFound['website'] : null, 'cover' => isset($pageFound['cover']) ? $pageFound['cover']->source : null));
} else {
$response['error'] = array('code' => 'SA03', 'type' => 'LiveAccessError', 'msg' => lang('fb_services_short_token_live'));
}
} else {
$response['error'] = array('code' => 'SA02', 'type' => 'NotFoundError', 'msg' => sprintf(lang('services_object_not_found'), 'FACEBOOK PAGE'));
}
} catch (\Exception $ex) {
$response['error'] = array('code' => $ex->getCode(), 'type' => method_exists($ex, 'getErrorType') ? $ex->getErrorType() : 'Error', 'msg' => $ex->getMessage());
}
}
}
}
return $response;
}