OdnoklassnikiOAuth2Service.php
3.2 KB
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
<?php
/**
* OdnoklassnikiOAuthService class file.
*
* Register application: http://dev.odnoklassniki.ru/wiki/pages/viewpage.action?pageId=13992188
* Manage your applications: http://www.odnoklassniki.ru/dk?st.cmd=appsInfoMyDevList&st._aid=Apps_Info_MyDev
* Note: Enabling this service a little more difficult because of the authorization policy of the service.
*
* @author Maxim Zemskov <nodge@yandex.ru>
* @link http://github.com/Nodge/yii2-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace common\components\nodge\eauth\src\services;
use nodge\eauth\oauth2\Service;
/**
* Odnoklassniki.Ru provider class.
*
* @package application.extensions.eauth.services
*/
class OdnoklassnikiOAuth2Service extends Service
{
const SCOPE_VALUABLE_ACCESS = 'VALUABLE ACCESS';
const SCOPE_SET_STATUS = 'SET STATUS';
const SCOPE_PHOTO_CONTENT = 'PHOTO CONTENT';
protected $name = 'odnoklassniki';
protected $title = 'Odnoklassniki';
protected $type = 'OAuth2';
protected $jsArguments = ['popup' => ['width' => 680, 'height' => 500]];
protected $clientPublic;
protected $scopes = [];
protected $scopeSeparator = ';';
protected $providerOptions = [
'authorize' => 'http://www.odnoklassniki.ru/oauth/authorize',
'access_token' => 'http://api.odnoklassniki.ru/oauth/token.do',
];
protected $baseApiUrl = 'http://api.odnoklassniki.ru/fb.do';
protected $tokenDefaultLifetime = 1500; // about 25 minutes
protected $validateState = false;
protected function fetchAttributes()
{
$info = $this->makeSignedRequest('', [
'query' => [
'method' => 'users.getCurrentUser',
'format' => 'JSON',
'application_key' => $this->clientPublic,
'client_id' => $this->clientId,
],
]);
$this->attributes['id'] = $info['uid'];
$this->attributes['name'] = $info['first_name'] . ' ' . $info['last_name'];
return true;
}
/**
* @return string
*/
public function getClientPublic()
{
return $this->clientPublic;
}
/**
* @param string $clientPublic
*/
public function setClientPublic($clientPublic)
{
$this->clientPublic = $clientPublic;
}
/**
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
*/
public function makeSignedRequest($url, $options = [], $parseResponse = true)
{
$token = $this->getAccessTokenData();
if (isset($token)) {
$params = '';
ksort($options['query']);
foreach ($options['query'] as $k => $v) {
$params .= $k . '=' . $v;
}
$options['query']['sig'] = md5($params . md5($token['access_token'] . $this->clientSecret));
$options['query']['access_token'] = $token['access_token'];
}
return parent::makeSignedRequest($url, $options, $parseResponse);
}
/**
* Returns the error array.
*
* @param array $response
* @return array the error array with 2 keys: code and message. Should be null if no errors.
*/
protected function fetchResponseError($response)
{
if (isset($response['error_code'])) {
return [
'code' => $response['error_code'],
'message' => $response['error_msg'],
];
} else {
return null;
}
}
}