|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SocialConnect project |
|
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace SocialConnect\Auth\Provider; |
|
8
|
|
|
|
|
9
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidResponse; |
|
10
|
|
|
use SocialConnect\OAuth2\AccessToken; |
|
11
|
|
|
use SocialConnect\Common\Entity\User; |
|
12
|
|
|
use SocialConnect\Common\Http\Client\Client; |
|
13
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
|
14
|
|
|
|
|
15
|
|
|
class Facebook extends \SocialConnect\OAuth2\AbstractProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $requestHttpMethod = Client::GET; |
|
23
|
|
|
|
|
24
|
|
|
public function getBaseUri() |
|
25
|
|
|
{ |
|
26
|
|
|
return 'https://graph.facebook.com/v2.8/'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getAuthorizeUri() |
|
30
|
|
|
{ |
|
31
|
|
|
return 'https://www.facebook.com/dialog/oauth'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getRequestTokenUri() |
|
35
|
|
|
{ |
|
36
|
|
|
return 'https://graph.facebook.com/oauth/access_token'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getName() |
|
40
|
|
|
{ |
|
41
|
|
|
return 'facebook'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getIdentity(AccessToken $accessToken) |
|
48
|
|
|
{ |
|
49
|
|
|
$response = $this->service->getHttpClient()->request( |
|
50
|
|
|
$this->getBaseUri() . 'me', |
|
51
|
|
|
[ |
|
52
|
|
|
'access_token' => $accessToken->getToken(), |
|
53
|
|
|
'fields' => $this->getFieldsInline() |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
if (!$response->isSuccess()) { |
|
58
|
|
|
throw new InvalidResponse( |
|
59
|
|
|
'API response with error code', |
|
60
|
|
|
$response |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$body = $response->getBody(); |
|
65
|
|
|
$result = json_decode($body); |
|
66
|
|
|
|
|
67
|
|
|
$hydrator = new ObjectMap(array( |
|
68
|
|
|
'id' => 'id', |
|
69
|
|
|
'first_name' => 'firstname', |
|
70
|
|
|
'last_name' => 'lastname', |
|
71
|
|
|
'email' => 'email', |
|
72
|
|
|
'gender' => 'sex', |
|
73
|
|
|
'link' => 'url', |
|
74
|
|
|
'locale' => 'locale', |
|
75
|
|
|
'name' => 'fullname', |
|
76
|
|
|
'timezone' => 'timezone', |
|
77
|
|
|
'updated_time' => 'dateModified', |
|
78
|
|
|
'verified' => 'verified' |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
return $hydrator->hydrate(new User(), $result); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|