/
VKontakte.php
145 lines (132 loc) · 3.48 KB
/
VKontakte.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\clients;
use yii\authclient\InvalidResponseException;
use yii\authclient\OAuth2;
use yii\helpers\Json;
/**
* VKontakte allows authentication via VKontakte OAuth.
*
* In order to use VKontakte OAuth you must register your application at <http://vk.com/editapp?act=create>.
*
* Example application configuration:
*
* ```php
* 'components' => [
* 'authClientCollection' => [
* '__class' => yii\authclient\Collection::class,
* 'clients' => [
* 'vkontakte' => [
* '__class' => yii\authclient\clients\VKontakte::class,
* 'clientId' => 'vkontakte_client_id',
* 'clientSecret' => 'vkontakte_client_secret',
* ],
* ],
* ]
* // ...
* ]
* ```
*
* @see http://vk.com/editapp?act=create
* @see http://vk.com/developers.php?oid=-1&p=users.get
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class VKontakte extends OAuth2
{
/**
* {@inheritdoc}
*/
public $authUrl = 'https://oauth.vk.com/authorize';
/**
* {@inheritdoc}
*/
public $tokenUrl = 'https://oauth.vk.com/access_token';
/**
* {@inheritdoc}
*/
public $apiBaseUrl = 'https://api.vk.com/method';
/**
* @var array list of attribute names, which should be requested from API to initialize user attributes.
* @since 2.0.4
*/
public $attributeNames = [
'uid',
'first_name',
'last_name',
'nickname',
'screen_name',
'sex',
'bdate',
'city',
'country',
'timezone',
'photo'
];
/**
* @var string the API version to send in the API request.
* @see https://vk.com/dev/versions
* @since 2.1.4
*/
public $apiVersion = '3.0';
/**
* {@inheritdoc}
*/
protected function initUserAttributes()
{
$response = $this->api('users.get.json', 'GET', [
'fields' => implode(',', $this->attributeNames),
]);
if (empty($response['response'])) {
throw new \RuntimeException('Unable to init user attributes due to unexpected response: ' . Json::encode($response));
}
$attributes = array_shift($response['response']);
$accessToken = $this->getAccessToken();
if (is_object($accessToken)) {
$accessTokenParams = $accessToken->getParams();
unset($accessTokenParams['access_token']);
unset($accessTokenParams['expires_in']);
$attributes = array_merge($accessTokenParams, $attributes);
}
return $attributes;
}
/**
* {@inheritdoc}
*/
public function applyAccessTokenToRequest($request, $accessToken)
{
$data = $request->getParams();
$data['v'] = $this->apiVersion;
$data['uids'] = $accessToken->getParam('user_id');
$data['access_token'] = $accessToken->getToken();
$request->setParams($data);
}
/**
* {@inheritdoc}
*/
protected function defaultName()
{
return 'vkontakte';
}
/**
* {@inheritdoc}
*/
protected function defaultTitle()
{
return 'VKontakte';
}
/**
* {@inheritdoc}
*/
protected function defaultNormalizeUserAttributeMap()
{
return [
'id' => 'uid'
];
}
}