forked from xjflyttp/yii2-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubanAuth.php
77 lines (64 loc) · 1.71 KB
/
DoubanAuth.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
<?php
namespace xj\oauth;
use yii\authclient\OAuth2;
use yii\base\Exception;
/**
* Douban OAuth
* @author light <[email protected]>
*/
class DoubanAuth extends OAuth2 implements IAuth {
/**
* @inheritdoc
*/
public $authUrl = 'https://www.douban.com/service/auth2/auth';
/**
* @inheritdoc
*/
public $tokenUrl = 'https://www.douban.com/service/auth2/token';
/**
* @inheritdoc
*/
public $apiBaseUrl = 'https://api.douban.com';
/**
* @inheritdoc
*/
public $scope = 'douban_basic_common';
/**
* Get authed user info
*
* @return array
* @see http://developers.douban.com/wiki/?title=user_v2#User
*/
public function getUserInfo() {
return $this->api('v2/user/~me', 'GET');
}
protected function defaultName() {
return 'douban';
}
protected function defaultTitle() {
return 'Douban';
}
protected function defaultViewOptions() {
return [
'popupWidth' => 800,
'popupHeight' => 500,
];
}
/**
*
* @ineritdoc
*/
public function api($apiSubUrl, $method = 'GET', array $params = [], array $headers = []) {
if (preg_match('/^https?:\\/\\//is', $apiSubUrl)) {
$url = $apiSubUrl;
} else {
$url = $this->apiBaseUrl . '/' . $apiSubUrl;
}
$accessToken = $this->getAccessToken();
if (!is_object($accessToken) || !$accessToken->getIsValid()) {
throw new Exception('Invalid access token.');
}
$headers[] = 'Authorization: Bearer ' . $accessToken->getToken();
return $this->apiInternal($accessToken, $url, $method, $params, $headers);
}
}