-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to store refresh token from email/password
- Loading branch information
1 parent
7f01fcf
commit 9fa373b
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
$argDefs = [ | ||
[ | ||
'name' => 'refreshTokenFile', | ||
'keys' => [ | ||
'r', | ||
'refresh-token', | ||
], | ||
'default' => '', | ||
], | ||
[ | ||
'name' => 'email', | ||
'keys' => [ | ||
'email', | ||
'e' | ||
], | ||
'default' => '' | ||
], | ||
[ | ||
'name' => 'password', | ||
'keys' => [ | ||
'password', | ||
'p' | ||
], | ||
'default' => '' | ||
] | ||
]; | ||
$h = getenv('HOME'); | ||
if ($h) { | ||
$argDefs[0]['default'] = $h.'/.cache/hwp/refresh-token.txt'; | ||
} | ||
|
||
$defaults = [ | ||
'OAUTH2_LOGIN_URL' => 'https://accounts.google.com/o/oauth2/programmatic_auth', | ||
'OAUTH2_TOKEN_REQUEST_URL' => 'https://accounts.google.com/o/oauth2/token', | ||
'OAUTH2_CLIENT_SECRET' => 'KWsJlkaMn1jGLxQpWxMnOox-', | ||
//'OAUTH2_LOGIN_URL' => 'https://accounts.google.com/o/oauth2/v2/auth', | ||
'OAUTH2_SCOPES' => [ | ||
'https://www.googleapis.com/auth/userinfo.email', | ||
'https://www.google.com/accounts/OAuthLogin', | ||
], | ||
'OAUTH2_CLIENT_ID' => '936475272427.apps.googleusercontent.com', | ||
|
||
]; | ||
$o = [ | ||
'password:', | ||
'email:', | ||
'refresh-token:', | ||
]; | ||
$options = getopt('p:e:r:', $o); | ||
$config = array_merge($defaults); | ||
|
||
foreach ($argDefs as $arg) { | ||
foreach ($options as $k => $v) { | ||
if (array_key_exists('default', $arg)) { | ||
$config[ $arg['name'] ] = $arg['default']; | ||
} else { | ||
$config[ $arg['name'] ] = NULL; | ||
} | ||
if (in_array($k, $arg['keys'])) { | ||
$config[ $arg['name'] ] = $v; | ||
continue 2; | ||
} | ||
} | ||
} | ||
|
||
include('vendor/autoload.php'); | ||
$session = new Requests_Session(); | ||
|
||
include('src/Client.php'); | ||
try { | ||
$client = new HWP\Client($session, $config); | ||
$authCode = $client->getAuthCode(); | ||
$tokenJson = $client->getTokensFromCode($authCode); | ||
$client->saveRefreshToken($tokenJson['refresh_token']); | ||
|
||
} catch (Exception $e) { | ||
echo "Error: ".$e->getMessage()."\n"; | ||
die(1); | ||
} | ||
|
||
|
||
|
||
function helpText() { | ||
echo "php ./bin/cli_auth_once.php [-e|--email youremail] [-p|--password \"pAssw0rd#\"] \n"; | ||
echo "Authenticate to Google and store a long term refresh token.\n"; | ||
echo "Parmeters:\n"; | ||
echo " -r --refresh-token: The filename to use as refresh token cache.\n"; | ||
echo " -e --email: Email for sign-in.\n"; | ||
echo " -p --password: Password for sign-in.\n"; | ||
} |