# Getting Access Token From The JavaScript SDK Example This example covers obtaining an access token and signed request from the Facebook JavaScript SDK with the Facebook SDK for PHP. ## Example {#example} In order to have the JavaScript SDK set a cookie containing a signed request (which contains information about the logged in user), you must first initialize the JavaScript SDK with the `{cookie: true}` option. ~~~~

Log In with the JavaScript SDK

~~~~ After the user successfully logs in, redirect the user (or make an AJAX request) to a PHP script that obtains an access token from the signed request that exists in the cookie. ~~~~ # /js-login.php $fb = new Facebook\Facebook([ 'app_id' => '{app-id}', 'app_secret' => '{app-secret}', 'default_graph_version' => 'v2.2', ]); $helper = $fb->getJavaScriptHelper(); 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)) { echo 'No cookie set or no OAuth data could be obtained from cookie.'; exit; } // Logged in echo '

Access Token

'; var_dump($accessToken->getValue()); $_SESSION['fb_access_token'] = (string) $accessToken; // User is logged in! // You can redirect them to a members-only page. //header('Location: https://example.com/members.php'); ~~~~