/
Facebook.php
116 lines (99 loc) · 2.99 KB
/
Facebook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
namespace SocialConnect\OAuth2\Provider;
use SocialConnect\OAuth2\AccessToken;
use SocialConnect\Provider\AccessTokenInterface;
use SocialConnect\Provider\Exception\InvalidAccessToken;
use SocialConnect\Provider\Exception\InvalidResponse;
use SocialConnect\Common\Entity\User;
use SocialConnect\Common\Http\Client\Client;
use SocialConnect\Common\Hydrator\ObjectMap;
class Facebook extends \SocialConnect\OAuth2\AbstractProvider
{
/**
* By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ
*
* @var string
*/
protected $requestHttpMethod = Client::GET;
public function getBaseUri()
{
return 'https://graph.facebook.com/v2.8/';
}
public function getAuthorizeUri()
{
return 'https://www.facebook.com/dialog/oauth';
}
public function getRequestTokenUri()
{
return 'https://graph.facebook.com/oauth/access_token';
}
public function getName()
{
return 'facebook';
}
/**
* {@inheritdoc}
*/
public function parseToken($body)
{
if (empty($body)) {
throw new InvalidAccessToken('Provider response with empty body');
}
$result = json_decode($body, true);
if ($result) {
return new AccessToken($result);
}
throw new InvalidAccessToken('Provider response with not valid JSON');
}
/**
* {@inheritdoc}
*/
public function getIdentity(AccessTokenInterface $accessToken)
{
$response = $this->httpClient->request(
$this->getBaseUri() . 'me',
[
'access_token' => $accessToken->getToken(),
'fields' => $this->getFieldsInline()
]
);
if (!$response->isSuccess()) {
throw new InvalidResponse(
'API response with error code',
$response
);
}
$result = $response->json();
if (!$result) {
throw new InvalidResponse(
'API response is not a valid JSON object',
$response
);
}
$result->picture_object = $result->picture;
$result->picture = $result->picture->data->url;
$hydrator = new ObjectMap(
[
'id' => 'id',
'first_name' => 'firstname',
'last_name' => 'lastname',
'email' => 'email',
'gender' => 'sex',
'link' => 'url',
'locale' => 'locale',
'name' => 'fullname',
'timezone' => 'timezone',
'updated_time' => 'dateModified',
'verified' => 'verified'
]
);
/** @var User $user */
$user = $hydrator->hydrate(new User(), $result);
$user->emailVerified = true;
return $user;
}
}