Skip to content

Commit b14edfa

Browse files
committed
Supported the new OAuth2 error format
1 parent 08909f3 commit b14edfa

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

src/facebook.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,20 @@ public function __construct($result) {
2828
$this->result = $result;
2929

3030
$code = isset($result['error_code']) ? $result['error_code'] : 0;
31-
$msg = isset($result['error'])
32-
? $result['error']['message'] : $result['error_msg'];
31+
32+
if (isset($result['error_description'])) {
33+
// OAuth 2.0 Draft 10 style
34+
$msg = $result['error_description'];
35+
} else if (isset($result['error']) && is_array($result['error'])) {
36+
// OAuth 2.0 Draft 00 style
37+
$msg = $result['error']['message'];
38+
} else if (isset($result['error_msg'])) {
39+
// Rest server style
40+
$msg = $result['error_msg'];
41+
} else {
42+
$msg = 'Unknown Error. Check getResult()';
43+
}
44+
3345
parent::__construct($msg, $code);
3446
}
3547

@@ -49,10 +61,19 @@ public function getResult() {
4961
* @return String
5062
*/
5163
public function getType() {
52-
return
53-
isset($this->result['error']) && isset($this->result['error']['type'])
54-
? $this->result['error']['type']
55-
: 'Exception';
64+
if (isset($this->result['error'])) {
65+
$error = $this->result['error'];
66+
if (is_string($error)) {
67+
// OAuth 2.0 Draft 10 style
68+
return $error;
69+
} else if (is_array($error)) {
70+
// OAuth 2.0 Draft 00 style
71+
if (isset($error['type'])) {
72+
return $error['type'];
73+
}
74+
}
75+
}
76+
return 'Exception';
5677
}
5778

5879
/**
@@ -79,7 +100,7 @@ class Facebook
79100
/**
80101
* Version.
81102
*/
82-
const VERSION = '2.1.1';
103+
const VERSION = '2.1.2';
83104

84105
/**
85106
* Default options for curl.
@@ -520,8 +541,12 @@ protected function _graph($path, $method='GET', $params=array()) {
520541
// results are returned, errors are thrown
521542
if (is_array($result) && isset($result['error'])) {
522543
$e = new FacebookApiException($result);
523-
if ($e->getType() === 'OAuthException') {
524-
$this->setSession(null);
544+
switch ($e->getType()) {
545+
// OAuth 2.0 Draft 00 style
546+
case 'OAuthException':
547+
// OAuth 2.0 Draft 10 style
548+
case 'invalid_token':
549+
$this->setSession(null);
525550
}
526551
throw $e;
527552
}

tests/tests.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class FacebookTest extends PHPUnit_Framework_TestCase
1010
const APP_ID = '117743971608120';
1111
const SECRET = '943716006e74d9b9283d4d5d8ab93204';
1212

13+
const MIGRATED_APP_ID = '148931871805121';
14+
const MIGRATED_SECRET = 'bb9b2bb536647ed3b92c1c9a8969ef7c';
15+
1316
private static $VALID_EXPIRED_SESSION = array(
1417
'access_token' => '117743971608120|2.vdCKd4ZIEJlHwwtrkilgKQ__.86400.1281049200-1677846385|NF_2DDNxFBznj2CuwiwabHhTAHc.',
1518
'expires' => '1281049200',
@@ -224,7 +227,6 @@ public function testInvalidSessionFromQueryString() {
224227

225228
$params = array(
226229
'fb_sig_in_iframe' => 1,
227-
'fb_sig_iframe_key' => '6512bd43d9caa6e02c990b0a82652dca',
228230
'fb_sig_user' => '1677846385',
229231
'fb_sig_session_key' =>
230232
'2.NdKHtYIuB0EcNSHOvqAKHg__.86400.1258092000-1677846385',
@@ -319,11 +321,11 @@ public function testGraphAPIWithSession() {
319321
} catch(FacebookApiException $e) {
320322
// means the server got the access token
321323
$msg = 'OAuthException: Error processing access token.';
322-
$this->assertEquals((string) $e, $msg,
324+
$this->assertEquals($msg, (string) $e,
323325
'Expect the invalid session message.');
324326
// also ensure the session was reset since it was invalid
325327
$this->assertEquals($facebook->getSession(), null,
326-
'Expect the to be reset.');
328+
'Expect the session to be reset.');
327329
}
328330
}
329331

@@ -339,7 +341,48 @@ public function testGraphAPIMethod() {
339341
} catch(FacebookApiException $e) {
340342
// ProfileDelete means the server understood the DELETE
341343
$msg = 'GraphMethodException: Unsupported delete request.';
342-
$this->assertEquals((string) $e, $msg,
344+
$this->assertEquals($msg, (string) $e,
345+
'Expect the invalid session message.');
346+
}
347+
}
348+
349+
public function testGraphAPIOAuthSpecError() {
350+
$facebook = new Facebook(array(
351+
'appId' => self::MIGRATED_APP_ID,
352+
'secret' => self::MIGRATED_SECRET,
353+
));
354+
355+
try {
356+
$response = $facebook->api('/me', array(
357+
'client_id' => self::MIGRATED_APP_ID));
358+
359+
$this->fail('Should not get here.');
360+
} catch(FacebookApiException $e) {
361+
// means the server got the access token
362+
$msg = 'invalid_request: An active access token must be used '.
363+
'to query information about the current user.';
364+
$this->assertEquals($msg, (string) $e,
365+
'Expect the invalid session message.');
366+
// also ensure the session was reset since it was invalid
367+
$this->assertEquals($facebook->getSession(), null,
368+
'Expect the session to be reset.');
369+
}
370+
}
371+
372+
public function testGraphAPIMethodOAuthSpecError() {
373+
$facebook = new Facebook(array(
374+
'appId' => self::MIGRATED_APP_ID,
375+
'secret' => self::MIGRATED_SECRET,
376+
));
377+
378+
try {
379+
$response = $facebook->api('/naitik', 'DELETE', array(
380+
'client_id' => self::MIGRATED_APP_ID));
381+
$this->fail('Should not get here.');
382+
} catch(FacebookApiException $e) {
383+
// ProfileDelete means the server understood the DELETE
384+
$msg = 'invalid_request: Unsupported delete request.';
385+
$this->assertEquals($msg, (string) $e,
343386
'Expect the invalid session message.');
344387
}
345388
}
@@ -350,6 +393,11 @@ public function testCurlFailure() {
350393
'secret' => self::SECRET,
351394
));
352395

396+
if (!defined('CURLOPT_TIMEOUT_MS')) {
397+
// can't test it if we don't have millisecond timeouts
398+
return;
399+
}
400+
353401
try {
354402
// we dont expect facebook will ever return in 1ms
355403
Facebook::$CURL_OPTS[CURLOPT_TIMEOUT_MS] = 1;
@@ -371,7 +419,7 @@ public function testGraphAPIWithOnlyParams() {
371419
'secret' => self::SECRET,
372420
));
373421

374-
$response = $facebook->api('/platform/feed',
422+
$response = $facebook->api('/331218348435/feed',
375423
array('limit' => 1, 'access_token' => ''));
376424
$this->assertEquals(1, count($response['data']), 'should get one entry');
377425
$this->assertTrue(

0 commit comments

Comments
 (0)