-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthGoogle.php
More file actions
81 lines (62 loc) · 2.78 KB
/
AuthGoogle.php
File metadata and controls
81 lines (62 loc) · 2.78 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
<?php
class AuthGoogle
{
public $client_id;
public $client_secret;
public $redirect_url;
public $login_url;
public $url_access_token = 'https://www.googleapis.com/oauth2/v4/token';
public $Curl_Connection;
public function __construct($client_id, $client_secret, $redirect_url)
{
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->redirect_url = $redirect_url;
$this->login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') . '&redirect_uri=' . urlencode($this->getRedirectUrl()) . '&response_type=code&client_id=' . $this->getClientId() . '&access_type=online';
}
public function getClientId()
{
return $this->client_id;
}
public function getClientSecret()
{
return $this->client_secret;
}
public function getRedirectUrl()
{
return $this->redirect_url;
}
public function getLoginUrl()
{
return $this->login_url;
}
public function getAccessToken($code)
{
$curlPost = 'client_id=' . $this->client_id . '&redirect_uri=' . $this->redirect_url . '&client_secret=' . $this->client_secret . '&code=' . $code . '&grant_type=authorization_code';
$this->Curl_Connection = curl_init();
curl_setopt($this->Curl_Connection, CURLOPT_URL, $this->url_access_token);
curl_setopt($this->Curl_Connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->Curl_Connection, CURLOPT_POST, true);
curl_setopt($this->Curl_Connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->Curl_Connection, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($this->Curl_Connection), true);
$http_code = curl_getinfo($this->Curl_Connection, CURLINFO_HTTP_CODE);
if ($http_code != 200)
throw new Exception('Error : Failed to receieve access token');
return $data;
}
public function GetUserProfileInfo($access_token)
{
$url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200)
throw new Exception('Error : Failed to get user information');
return $data;
}
}