Skip to content

Commit 1cd2bcc

Browse files
authored
update to 2.6 and add fields
As of 2.4 you need to pass the 'fields' parameter in order to get the user email and other info
1 parent 813b84d commit 1cd2bcc

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

FacebookStrategy.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
/**
33
* Facebook strategy for Opauth
44
* based on https://developers.facebook.com/docs/authentication/server-side/
5-
*
5+
*
66
* More information on Opauth: http://opauth.org
7-
*
7+
*
88
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
99
* @link http://opauth.org
1010
* @package Opauth.FacebookStrategy
1111
* @license MIT License
1212
*/
1313

1414
class FacebookStrategy extends OpauthStrategy{
15-
15+
1616
/**
1717
* Compulsory config keys, listed as unassociative arrays
1818
* eg. array('app_id', 'app_secret');
1919
*/
2020
public $expects = array('app_id', 'app_secret');
21-
21+
2222
/**
2323
* Optional config keys with respective default values, listed as associative arrays
2424
* eg. array('scope' => 'email');
@@ -42,10 +42,10 @@ public function request(){
4242
if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type'];
4343
if (!empty($this->strategy['display'])) $params['display'] = $this->strategy['display'];
4444
if (!empty($this->strategy['auth_type'])) $params['auth_type'] = $this->strategy['auth_type'];
45-
45+
4646
$this->clientGet($url, $params);
4747
}
48-
48+
4949
/**
5050
* Internal callback, after Facebook's OAuth
5151
*/
@@ -59,7 +59,7 @@ public function int_callback(){
5959
'code' => trim($_GET['code'])
6060
);
6161
$response = $this->serverGet($url, $params, null, $headers);
62-
62+
6363
parse_str($response, $results);
6464

6565
if (!empty($results) && !empty($results['access_token'])){
@@ -70,29 +70,27 @@ public function int_callback(){
7070
'uid' => $me->id,
7171
'info' => array(
7272
'name' => $me->name,
73-
'image' => 'https://graph.facebook.com/'.$me->id.'/picture?type=square'
73+
'image' => 'https://graph.facebook.com/v2.6/'.$me->id.'/picture?type=large'
7474
),
7575
'credentials' => array(
7676
'token' => $results['access_token'],
7777
'expires' => date('c', time() + $results['expires'])
7878
),
7979
'raw' => $me
8080
);
81-
81+
8282
if (!empty($me->email)) $this->auth['info']['email'] = $me->email;
83-
if (!empty($me->username)) $this->auth['info']['nickname'] = $me->username;
83+
if (!empty($me->name)) $this->auth['info']['nickname'] = $me->name;
8484
if (!empty($me->first_name)) $this->auth['info']['first_name'] = $me->first_name;
8585
if (!empty($me->last_name)) $this->auth['info']['last_name'] = $me->last_name;
86-
if (!empty($me->location)) $this->auth['info']['location'] = $me->location->name;
8786
if (!empty($me->link)) $this->auth['info']['urls']['facebook'] = $me->link;
88-
if (!empty($me->website)) $this->auth['info']['urls']['website'] = $me->website;
89-
87+
9088
/**
9189
* Missing optional info values
9290
* - description
9391
* - phone: not accessible via Facebook Graph API
9492
*/
95-
93+
9694
$this->callback();
9795
}
9896
else{
@@ -113,19 +111,26 @@ public function int_callback(){
113111
'message' => $_GET['error_description'],
114112
'raw' => $_GET
115113
);
116-
114+
117115
$this->errorCallback($error);
118116
}
119117
}
120-
118+
121119
/**
122120
* Queries Facebook Graph API for user info
123121
*
124-
* @param string $access_token
122+
* @param string $access_token
125123
* @return array Parsed JSON results
126124
*/
127125
private function me($access_token){
128-
$me = $this->serverGet('https://graph.facebook.com/me', array('access_token' => $access_token), null, $headers);
126+
127+
$fields = 'id,name,email';//default value
128+
if ( isset($this->strategy['fields']) ) {
129+
$fields = $this->strategy['fields'];
130+
}
131+
132+
$me = $this->serverGet('https://graph.facebook.com/v2.6/me', array('access_token' => $access_token, 'fields' => $fields), null, $headers);
133+
129134
if (!empty($me)){
130135
return json_decode($me);
131136
}
@@ -143,4 +148,4 @@ private function me($access_token){
143148
$this->errorCallback($error);
144149
}
145150
}
146-
}
151+
}

0 commit comments

Comments
 (0)