Skip to content

Commit 9513f08

Browse files
committed
Extract the code from the signed_request for JS SDK pairing
1 parent 54acf92 commit 9513f08

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Facebook PHP SDK (v.3.1.0)
1+
Facebook PHP SDK (v.3.1.1)
22
==========================
33

44
The [Facebook Platform](http://developers.facebook.com/) is

src/base_facebook.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ abstract class BaseFacebook
120120
/**
121121
* Version.
122122
*/
123-
const VERSION = '3.1.0';
123+
const VERSION = '3.1.1';
124124

125125
/**
126126
* Default options for curl.
@@ -337,11 +337,23 @@ protected function getUserAccessToken() {
337337
// the access token.
338338
$signed_request = $this->getSignedRequest();
339339
if ($signed_request) {
340+
// apps.facebook.com hands the access_token in the signed_request
340341
if (array_key_exists('oauth_token', $signed_request)) {
341342
$access_token = $signed_request['oauth_token'];
342343
$this->setPersistentData('access_token', $access_token);
343344
return $access_token;
344345
}
346+
347+
// the JS SDK puts a code in with the redirect_uri of ''
348+
if (array_key_exists('code', $signed_request)) {
349+
$code = $signed_request['code'];
350+
$access_token = $this->getAccessTokenFromCode($code, '');
351+
if ($access_token) {
352+
$this->setPersistentData('code', $code);
353+
$this->setPersistentData('access_token', $access_token);
354+
return $access_token;
355+
}
356+
}
345357

346358
// signed request states there's no access token, so anything
347359
// stored should be cleared.
@@ -635,11 +647,15 @@ protected function establishCSRFTokenState() {
635647
* @return mixed An access token exchanged for the authorization code, or
636648
* false if an access token could not be generated.
637649
*/
638-
protected function getAccessTokenFromCode($code) {
650+
protected function getAccessTokenFromCode($code, $redirect_uri = null) {
639651
if (empty($code)) {
640652
return false;
641653
}
642654

655+
if ($redirect_uri === null) {
656+
$redirect_uri = $this->getCurrentUrl();
657+
}
658+
643659
try {
644660
// need to circumvent json_decode by calling _oauthRequest
645661
// directly, since response isn't JSON format.
@@ -648,7 +664,7 @@ protected function getAccessTokenFromCode($code) {
648664
$this->getUrl('graph', '/oauth/access_token'),
649665
$params = array('client_id' => $this->getAppId(),
650666
'client_secret' => $this->getApiSecret(),
651-
'redirect_uri' => $this->getCurrentUrl(),
667+
'redirect_uri' => $redirect_uri,
652668
'code' => $code));
653669
} catch (FacebookApiException $e) {
654670
// most likely that user very recently revoked authorization.

0 commit comments

Comments
 (0)