1 | <?php |
||
2 | /** |
||
3 | * Google Api 控制台 https://console.developers.google.com |
||
4 | * Oauth开发文档: |
||
5 | * https://developers.google.com/identity/protocols/OAuth2?csw=1 |
||
6 | * https://developers.google.com/identity/protocols/OAuth2WebServer |
||
7 | * App开发文档: |
||
8 | * https://developers.google.com/identity/sign-in/android/offline-access |
||
9 | * 1.创建项目->并创建凭证 |
||
10 | */ |
||
11 | namespace tinymeng\OAuth2\Gateways; |
||
12 | |||
13 | use tinymeng\OAuth2\Connector\Gateway; |
||
14 | use tinymeng\OAuth2\Exception\OAuthException; |
||
15 | use tinymeng\OAuth2\Helper\ConstCode; |
||
16 | |||
17 | /** |
||
18 | * Class Google |
||
19 | * @package tinymeng\OAuth2\Gateways |
||
20 | * @Author: TinyMeng <[email protected]> |
||
21 | * @Created: 2018/11/9 |
||
22 | */ |
||
23 | class Google extends Gateway |
||
24 | { |
||
25 | const API_BASE = 'https://www.googleapis.com/'; |
||
26 | const AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/v2/auth'; |
||
27 | protected $AccessTokenURL = 'https://www.googleapis.com/oauth2/v4/token'; |
||
28 | |||
29 | /** |
||
30 | * 得到跳转地址 |
||
31 | */ |
||
32 | public function getRedirectUrl() |
||
33 | { |
||
34 | //存储state |
||
35 | $this->saveState(); |
||
36 | //登录参数 |
||
37 | $params = [ |
||
38 | 'client_id' => $this->config['app_id'], |
||
39 | 'redirect_uri' => $this->config['callback'], |
||
40 | 'response_type' => $this->config['response_type'], |
||
41 | 'scope' => $this->config['scope'], |
||
42 | 'state' => $this->config['state'], |
||
43 | ]; |
||
44 | return self::AUTHORIZE_URL . '?' . http_build_query($params); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * 获取当前授权用户的openid标识 |
||
49 | */ |
||
50 | public function openid() |
||
51 | { |
||
52 | $userInfo = $this->getUserInfo(); |
||
53 | return $userInfo['id']; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * 获取格式化后的用户信息 |
||
58 | */ |
||
59 | public function userInfo() |
||
60 | { |
||
61 | $result = $this->getUserInfo(); |
||
62 | |||
63 | $userInfo = array( |
||
64 | 'open_id' => $this->token['access_token'], |
||
65 | 'access_token'=> $this->token['access_token'] ?? '', |
||
66 | 'union_id'=> $result['id'], |
||
67 | 'channel' => ConstCode::TYPE_GOOGLE, |
||
68 | 'nickname'=> $result['name'] ?? $result['email'], |
||
69 | 'gender' => isset($result['gender']) ? $this->getGender($result['gender']) : ConstCode::GENDER, |
||
70 | 'avatar' => $result['picture'], |
||
71 | 'native' => $result, |
||
72 | ); |
||
73 | if(isset($result['email'])){ |
||
74 | $userInfo['email'] = $result['email']; |
||
75 | } |
||
76 | return $userInfo; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * 获取原始接口返回的用户信息 |
||
81 | */ |
||
82 | public function getUserInfo() |
||
83 | { |
||
84 | if($this->type == 'app'){//App登录 |
||
85 | if(!isset($_REQUEST['code']) ){ |
||
86 | throw new OAuthException("Google APP登录 需要传输code参数! "); |
||
87 | } |
||
88 | } |
||
89 | $this->getToken(); |
||
90 | $headers = ['Authorization: Bearer ' . $this->token['access_token']]; |
||
91 | $data = $this->get(self::API_BASE . 'oauth2/v2/userinfo', '', $headers); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
92 | return json_decode($data, true); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Description: 解析access_token方法请求后的返回值 |
||
97 | * @author: JiaMeng <[email protected]> |
||
98 | * Updater: |
||
99 | * @param $token |
||
100 | * @return mixed |
||
101 | * @throws OAuthException |
||
102 | */ |
||
103 | protected function parseToken($token) |
||
104 | { |
||
105 | $data = json_decode($token, true); |
||
106 | if (isset($data['access_token'])) { |
||
107 | return $data; |
||
108 | } else { |
||
109 | throw new OAuthException("获取谷歌 ACCESS_TOKEN 出错:{$token}"); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 |