forked from magnetikonline/php-google-spreadsheet-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchangecodefortokens.php
More file actions
executable file
·63 lines (47 loc) · 1.44 KB
/
Copy pathexchangecodefortokens.php
File metadata and controls
executable file
·63 lines (47 loc) · 1.44 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
#!/usr/bin/env php
<?php
require('base.php');
require('oauth2/token.php');
require('oauth2/googleapi.php');
class ExchangeCodeForTokens extends Base {
public function execute() {
// get code from CLI parameter - exit if false (bad/no data)
if (($authCode = $this->getAuthCodeFromCLI()) === false) return;
$OAuth2GoogleAPI = $this->getOAuth2GoogleAPIInstance();
// make request for OAuth2 tokens
echo(sprintf(
"Requesting OAuth2 tokens via authorization code: %s\n",
$authCode
));
try {
$tokenData = $OAuth2GoogleAPI->getAccessTokenFromAuthCode($authCode);
// save token data to disk
echo(sprintf(
"Success! Saving token data to [%s]\n",
$this->config['tokenDataFile']
));
$this->saveOAuth2TokenData($tokenData);
// all done
} catch (Exception $e) {
// token fetch error
echo(sprintf("Error: %s\n",$e->getMessage()));
}
}
private function getAuthCodeFromCLI() {
// attempt to get code from CLI
if (!($optList = getopt('c:'))) {
// no -c switch or switch without value
echo(sprintf("Usage: %s -c AUTHORIZATION CODE\n",basename(__FILE__)));
return false;
}
// validate authorization code format
$authCode = $optList['c'];
if (!preg_match('/^[0-9A-Za-z._\/-]{30,62}$/',$authCode)) {
echo("Error: invalid Google authorization code format\n\n");
return false;
}
// all valid text wise
return $authCode;
}
}
(new ExchangeCodeForTokens(require('config.php')))->execute();