ggdrv/tokens.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 91 CC 7 SCAR 21.8
CC
7
Cov
0%
tokens
20.0
get_tokens
12.0
atoken
6.0
CRAP 8.9
1 package App::ggdrv::tokens ; #use strict;
2 use warnings;
3 use 5.030 ;
4 use feature 'say' ;
5 use Net::Google::OAuth ; #use Exporter 'import';
6 use POSIX qw [ strftime ] ;
7 use Term::ANSIColor qw[ color :constants ] ; $Term::ANSIColor::AUTORESET = 1 ;
8  
9  
10 my ($gfile, $cid, $csec, $email, $scope, $rtoken0, $atoken0 ) ;
11 return 1 ;
12  
13 sub tokens ( $$$ ) {
14 my $get = $_[0] ;
15 my $try = $_[1] ;
16 my $atoken = $_[2] ;
17 $gfile = ( $ENV{ GGDRV_API } // "~/.ggdrv2303v1" ) ;
18 #open my $FH , '<' , $gfile ; while (<$FH>){say $_ } ;
19 my $mtime = [ stat $gfile ]->[9] ; # 更新時刻 ## $gfile がコマンドラインからは見えるのに、このプログラムからは見えない不具合あり。
20 #say "@tmp" ;
21 #say qx [ ls -l $gfile ] ; exit ;
22 $cid = qx [ sed -ne's/^CLIENT_ID[ =:\t]*//p' $gfile ] =~ s/\n$//r ; #"54525797.....34dseo.apps.googleusercontent.com" ;
23 $csec = qx [ sed -ne's/^CLIENT_SECRET[ =:\t]*//p' $gfile ] =~ s/\n$//r ; # "GOCSP...YUbpe1" ;
24 $email = qx [ sed -ne's/^EMAIL[ =:\t]*//p' $gfile ] =~ s/\n$//r ;
25 $scope = 'drive'; #my $SCOPE = 'spreadsheets';
26 $rtoken0 = qx [ sed -ne's/^REFRESH_TOKEN[ =:\t]*//p' $gfile ] =~ s/\n$//r ; #"1//0e8......yLDJyrKxXNJY" ;
27 $atoken0 = qx [ sed -ne's/^ACCESS_TOKEN[ =:\t]*//p' $gfile ] =~ s/\n$//r ; #"1//0e8......yLDJyrKxXNJY" ;
28 $atoken ? atoken ($try) : $get ? get_tokens () : show_tokens ( $try ) ;
29 say "Modified time of the setup variable file : " , strftime( "%Y-%m-%d %H:%M:%S." , localtime $mtime ) if defined $mtime ;
30 1 ;
31 }
32  
33 sub show_tokens () {
34 say REVERSE "REFRESH TOKEN from the setup file: " ;
35 say $rtoken0 , " (". length($rtoken0)." chars)" ;
36 say REVERSE "ACCESS TOKEN from the setup file: " ;
37 say $atoken0 , " (". length($atoken0)." chars)" ;
38 1 ;
39 }
40  
41 # アクセストークンとリフレッシュトークンを獲得する。
42 sub get_tokens ( $ ) {
43 my $try = $_[0] ;
44 say YELLOW "次の英文をよく読み、それを実行せよ。途中で「続行」のボタンを2回押すことになるであろう。" ;
45 say 'Paste the following url into your browser. Push "Continue" button twice. Then copy the URL on your browser to paste here.' ;
46 my $oauth = Net::Google::OAuth->new( -client_id => $cid, -client_secret => $csec ) ;
47 $oauth->generateAccessToken( -scope => $scope, -email => $email ) ;
48 my $atoken = $oauth -> getAccessToken () ;
49 my $rtoken = $oauth -> getRefreshToken () ;
50 print "This is ACCESS TOKEN:\n"; print "=" x 20 . "\n"; print $atoken . "\n"; print "=" x 20 . "\n" ;
51 print "This is REFRESH TOKEN:\n"; print "=" x 20 . "\n"; print $rtoken . "\n"; print "=" x 20 . "\n" ;
52 qx [ sed -i.bak -e's|^\\(REFRESH_TOKEN[ =:\t]*\\).*\$|\\1$rtoken|' $gfile ] if ! $try ;
53 qx [ sed -i.bak -e's/^\\(ACCESS_TOKEN[ =:\t]*\\).*\$/\\1$atoken/' $gfile ] if ! $try ;
54 1 ;
55 }
56  
57 # クライアントIDとクライアントシークレット、リフレッシュトークン(計3個の情報)から、アクセストークンを取得する。
58 sub atoken ( $ ) {
59 my $try = $_[0] ;
60 my $oauth = Net::Google::OAuth->new( -client_id => $cid, -client_secret => $csec ) ;
61 my $x1 = $oauth -> refreshToken ( -refresh_token => $rtoken0 ) ;
62 my $atoken = $oauth -> getAccessToken () ;
63 say $atoken ;
64 qx [ sed -i.bak -e's/^\\(ACCESS_TOKEN[ =:\t]*\\).*\$/\\1$atoken/' $gfile ] if ! $try ;
65 # qxが\を解釈するので、この行を編集するときは要注意。
66 # qxに sed で行末を表す$を渡す際に、$が何かPerlの変数として解釈されないように、\が前に必要。
67 # sed では Mac だと -i に引数が必要。
68 # sed では、\1 にキャプチャするための括弧は、元々\が必要。それをqxに渡す場合に\をさらに前に追加。
69 }
70