Issues (27)

src/Gateways/Kuaishou.php (3 issues)

Labels
1
<?php
2
/**
3
 * 快手开放平台 https://open.kuaishou.com/
4
 * api接口文档: https://open.kuaishou.com/platform/docs/api-overview
5
 */
6
namespace tinymeng\OAuth2\Gateways;
7
8
use tinymeng\OAuth2\Connector\Gateway;
9
use tinymeng\OAuth2\Exception\OAuthException;
10
use tinymeng\OAuth2\Helper\ConstCode;
11
12
class Kuaishou extends Gateway
13
{
14
    const API_BASE = 'https://open.kuaishou.com/';
15
    protected $AuthorizeURL = 'https://open.kuaishou.com/oauth2/authorize';
16
    protected $AccessTokenURL = 'https://open.kuaishou.com/oauth2/access_token';
17
    protected $UserInfoURL = 'https://open.kuaishou.com/openapi/user_info';
18
19
    /**
20
     * 得到跳转地址
21
     */
22
    public function getRedirectUrl()
23
    {
24
        $this->switchAccessTokenURL();
0 ignored issues
show
The method switchAccessTokenURL() does not exist on tinymeng\OAuth2\Gateways\Kuaishou. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        $this->/** @scrutinizer ignore-call */ 
25
               switchAccessTokenURL();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        $params = [
26
            'app_id'        => $this->config['app_id'],
27
            'redirect_uri'  => $this->config['callback'],
28
            'response_type' => $this->config['response_type'],
29
            'scope'         => $this->config['scope'] ?: 'user_info',
30
            'state'         => $this->config['state'] ?: '',
31
        ];
32
        return $this->AuthorizeURL . '?' . http_build_query($params);
33
    }
34
35
    /**
36
     * 获取当前授权用户的openid标识
37
     */
38
    public function openid()
39
    {
40
        $this->getToken();
41
        return $this->token['open_id'] ?? '';
42
    }
43
44
    /**
45
     * 获取格式化后的用户信息
46
     */
47
    public function userInfo()
48
    {
49
        $result = $this->getUserInfo();
50
51
        $userInfo = [
52
            'open_id'      => $this->openid(),
53
            'union_id'     => $this->token['union_id'] ?? '',
54
            'channel'      => ConstCode::TYPE_KUAISHOU,
55
            'nickname'     => $result['name'] ?? '',
56
            'gender'       => $result['sex'] ?? ConstCode::GENDER,
57
            'avatar'       => $result['head'] ?? '',
58
            'type'         => ConstCode::getTypeConst(ConstCode::TYPE_KUAISHOU, $this->type),
59
            'access_token' => $this->token['access_token'] ?? '',
60
            'native'       => $result
61
        ];
62
        return $userInfo;
63
    }
64
65
    /**
66
     * 获取原始接口返回的用户信息
67
     */
68
    public function getUserInfo()
69
    {
70
        $this->getToken();
71
        
72
        $params = [
73
            'app_id'       => $this->config['app_id'],
74
            'access_token' => $this->token['access_token'],
75
        ];
76
        
77
        $data = $this->get($this->UserInfoURL, $params);
78
        $data = json_decode($data, true);
79
        
80
        if (!isset($data['result'])) {
81
            throw new OAuthException('获取快手用户信息失败:' . json_encode($data));
82
        }
83
        return $data['result'];
84
    }
85
86
    /**
87
     * 获取access_token
88
     */
89
    protected function getAccessToken()
90
    {
91
        $params = [
92
            'app_id'       => $this->config['app_id'],
93
            'app_secret'   => $this->config['app_secret'],
94
            'code'         => isset($_REQUEST['code']) ? $_REQUEST['code'] : '',
95
            'grant_type'   => 'authorization_code',
96
        ];
97
        
98
        $response = $this->post($this->AccessTokenURL, $params);
99
        $response = json_decode($response, true);
0 ignored issues
show
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
100
        
101
        if (!isset($response['access_token'])) {
102
            throw new OAuthException('获取快手 access_token 出错:' . json_encode($response));
103
        }
104
        return $response;
105
    }
106
107
    /**
108
     * 检验授权凭证AccessToken是否有效
109
     */
110
    public function validateAccessToken($accessToken = null)
111
    {
112
        try {
113
            $accessToken = $accessToken ?? $this->token['access_token'];
114
            $params = [
115
                'app_id'       => $this->config['app_id'],
116
                'access_token' => $accessToken,
117
            ];
118
            $data = $this->get($this->UserInfoURL, $params);
119
            $data = json_decode($data, true);
120
            return isset($data['result']);
121
        } catch (\Exception $e) {
122
            return false;
123
        }
124
    }
125
126
    /**
127
     * 刷新AccessToken续期
128
     */
129
    public function refreshToken($refreshToken)
130
    {
131
        $params = [
132
            'app_id'        => $this->config['app_id'],
133
            'app_secret'    => $this->config['app_secret'],
134
            'refresh_token' => $refreshToken,
135
            'grant_type'    => 'refresh_token',
136
        ];
137
        
138
        $response = $this->post($this->AccessTokenURL, $params);
139
        $response = json_decode($response, true);
0 ignored issues
show
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
140
        
141
        if (isset($response['access_token'])) {
142
            $this->token = $response;
143
            return true;
144
        }
145
        return false;
146
    }
147
}