-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
68 lines (55 loc) · 1.8 KB
/
test.php
File metadata and controls
68 lines (55 loc) · 1.8 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
<?php
// https://developers.google.com/identity/protocols/oauth2/service-account#httprest
require_once("GoogleServiceAccount.php");
require_once("JWT.php");
require_once("FileTokenCache.php");
require_once("HttpClient.php");
class JWTCreator implements \GSAToken\JWTFactory
{
public function create($header, $claims, $secretkey)
{
return createJWT($header, $claims, $secretkey);
}
}
class CurlHttpClient implements \GSAToken\HttpClient
{
protected $client = null;
public function __construct()
{
$this->client = new HttpClient();
}
public function request($method, $url, $headers, $postdata)
{
return $this->client->request($method, $url, $headers, $postdata);
}
}
// Config file for a Google Service Account
$configfile = __DIR__."/serviceaccount.json";
// Name of a file where to store the cached token
$cachefile = __DIR__."/accesstoken.cache";
$gsa = new \GSAToken\GoogleServiceAccount($configfile);
$tokencache = new FileTokenCache($cachefile);
// echo base64_encode(openssl_random_pseudo_bytes(32));
// echo base64_encode(openssl_random_pseudo_bytes(64));
$enc_key = base64_decode("sz2D2lR8zPSCRHl/YZcYYBi1xFOW003rtjn5foCG/Qg=");
$mac_key = base64_decode("V7oKzLdgWjHQftXs70sOgzXksuiwHPigf4cLE6qPdic+Y6zrR6pvzN+RqS2K/i2iB2sajvgIElmLGGa3KoLemw==");
$tokencache->setPassphrase($enc_key, $mac_key);
$gsa->setTokenCache($tokencache);
$gsa->setJWTFactory(new JWTCreator());
$gsa->setHttpClient(new CurlHttpClient());
$now = time();
$scopes = array("https://www.googleapis.com/auth/firebase.messaging");
$ret = $gsa->fetchAccessToken($scopes);
$accesstoken = $ret['access_token'];
$ret['expires_in'] = $ret['expires_at'] > $now ? $ret['expires_at'] - $now : 0;
print_r($ret);
/*
Array
(
[access_token] => ya29.c.c0AY.....
[expires_in] => 3599
[token_type] => Bearer
[expires_at] => 1698841702
)
*/
exit;