-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoogleOAuth2.php
More file actions
77 lines (58 loc) · 2.61 KB
/
GoogleOAuth2.php
File metadata and controls
77 lines (58 loc) · 2.61 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
<?php
/*
* Thid can be used in PHPMailer
* add implements PHPMailer\PHPMailer\OAuthTokenProvider and set $userEmail
*/
class GoogleOAuth2 {
private string $_accessToken = '';
public function __construct(private string $clientID,
private string $clientSecret,
private string $refreshToken,
private string $userEmail = '') {}
public function fetchAccessToken(): string {
$postData = http_build_query(['grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
'client_id' => $this->clientID,
'client_secret' => $this->clientSecret]);
$options = ['http' => ['protocol_version' => 1.1,
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded',
'Cache-Control: no-store'],
'content' => $postData]];
$result = file_get_contents('https://oauth2.googleapis.com/token', false, stream_context_create($options));
$accessToken = json_decode($result, true);
$this->_accessToken = $accessToken['access_token'];
return $this->_accessToken;
}
public function getOauth64(): string
{
$this->fetchAccessToken();
return (base64_encode("user={$this->userEmail}\1auth=Bearer {$this->_accessToken}\1\1"));
}
public function generatePermissionURL(string $redirectURL = "http://localhost",
string $accountsURL = "https://accounts.google.com/o/oauth2/auth",
string $scope = "https://mail.google.com"): string
{
$getParams = http_build_query(['client_id' => $this->clientID,
'redirect_uri' => $redirectURL,
'scope' => $scope,
'response_type' => 'code',
'access_type' => 'offline',
'prompt' => 'consent']);
return ("{$accountsURL}?{$getParams}");
}
public function getRefreshToken(string $authorizationCode, string $redirectURL = "http://localhost"): string {
$postData = http_build_query(['grant_type' => 'authorization_code',
'redirect_uri' => $redirectURL,
'code' => $authorizationCode,
'client_id' => $this->clientID,
'client_secret' => $this->clientSecret]);
$options = ['http' => ['protocol_version' => 1.1,
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded',
'Cache-Control: no-store'],
'content' => $postData]];
$result = file_get_contents('https://oauth2.googleapis.com/token', false, stream_context_create($options));
return ($result);
}
}