' ' gmail oauth2パラメータ取得スクリプト(gmail-oauth2.vbs) ' 2020.6.24 B21Soft, Inc. ' 2022.3.23 更新 ブラウザを Chrome に変更 BTYPE="c" ' c:Chrome e:Edge i:IE ' redirect_uri=urn:ietf:wg:oauth:2.0:oob を http://localhost/notarealpage に変更 ''============================================== '' ログファイル LOGFILE="C:\●●slog.txt" '' curl トレースファイル CURLTRACE1="--trace-ascii C:\●●clog1.txt" CURLTRACE2="--trace-ascii C:\●●clog2.txt" 'クライアントID CID="●●" 'クライアントシークレット CSS="●●" '============================================== ' フェーズ@で取得した認証コード 初期値は"" ACC="" ' フェーズAで取得したリフレッシュトークン 初期値は"" REF="" '====================================================== Set objShell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") Set logf = fso.OpenTextFile(LOGFILE, 8, true) ' ' @認証コード取得フェーズ If ACC = "" then Putlog Date() & " " & Time() & ":@認証コード取得フェーズ" URL1="https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=#CID#" & _ "&redirect_uri=http://localhost/notarealpage&scope=https://mail.google.com/&access_type=offline" ret=Replace(URL1,"#CID#",CID) Putlog ret ' ブラウザが開くので、認証コードを取得して 変数ACCにセット BTYPE="e" ' c:Chrome e:Edge i:IE If BTYPE = "e" Then objShell.Exec "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & ret Wscript.Quit End If If BTYPE = "e" Then CreateObject("Shell.Application").ShellExecute "microsoft-edge:" & ret Wscript.Quit End If If BTYPE = "i" Then set IE = CreateObject ("InternetExplorer.Application") IE.Visible = True IE.Navigate(ret) End If Wscript.Quit End If ' Aリフレッシュトークン取得フェーズ If REF = "" Then Putlog Date() & " " & Time() & ":Aリフレッシュトークン取得フェーズ" CURL1="curl " & CURLTRACE1 & " -d ""code=#ACC#"" -d ""client_id=#CID#"" -d ""client_secret=#CSS#""" & _ " -d ""redirect_uri=http://localhost/notarealpage"" -d ""grant_type=authorization_code""" & _ " -d ""access_type=offline"" https://accounts.google.com/o/oauth2/token" ret=Replace(CURL1,"#ACC#",ACC) ret=Replace(ret,"#CID#",CID) ret=Replace(ret,"#CSS#",CSS) Putlog ret ' refresh_token内容を 変数REFに設定 ' Set objExec = objShell.Exec(ret) Putlog "" Putlog "curl grant_type=authorization_code..... done." Putlog "" Do While objExec.Status = 0 WScript.Sleep 100 Loop Do Until objExec.StdOut.AtEndOfStream strLine = objExec.StdOut.ReadLine Putlog strLine If REF = "" Then REF = Get_Token(strLine,"refresh_token") End If If ATK = "" Then ATK = Get_Token(strLine,"access_token") End If Loop If ATK <> "" Then Putlog "access_token=" & ATK End If If REF <> "" Then Putlog "refresh_token内容を 変数REFに設定" Putlog "refresh_token=" & REF End If Putlog "" Putlog "done." Wscript.Quit End If ' Bリフレッシュトークンによるアクセストークン取得テスト If REF <> "" Then Putlog Date() & " " & Time() & ":Bリフレッシュトークンによるアクセストークン取得テスト" CURL2="curl " & CURLTRACE2 & " -d ""client_id=#CID#"" -d ""client_secret=#CSS#""" & _ " -d ""grant_type=refresh_token"" -d ""refresh_token=#REF#""" & _ " https://accounts.google.com/o/oauth2/token" ret=Replace(CURL2,"#CID#",CID) ret=Replace(ret,"#CSS#",CSS) ret=Replace(ret,"#REF#",REF) Putlog ret Set objExec = objShell.Exec(ret) Putlog "" Putlog "curl grant_type=refresh_token..... done." Putlog "" Do While objExec.Status = 0 WScript.Sleep 100 Loop Do Until objExec.StdOut.AtEndOfStream strLine = objExec.StdOut.ReadLine Putlog strLine If ATK = "" Then ATK = Get_Token(strLine,"access_token") End If Loop If ATK <> "" Then Putlog "" Putlog "リフレッシュトークンによるアクセストークン取得テスト完了" Putlog "access_token=" & ATK End If Putlog "" Putlog "-------------- basp21p.ini -------------" Putlog "[gmail]" Putlog "# " & Date() & " Gmail OAuth2" Putlog "allow=all" Putlog "sslver=12" Putlog "client_id=" & CID Putlog "client_secret=" & CSS Putlog "token_uri=https://accounts.google.com/o/oauth2/token" Putlog "refresh_token=" & REF Putlog "----------------------------------------" Putlog "done." Wscript.Quit End If Function Get_Token(line,key) Get_Token = "" key2 = """" & key & """" i = InStr(line,key2) If i > 0 Then wk = Mid(line,i+len(key2)) i = InStr(wk,"""") wk = Mid(wk,i+1) i = InStr(wk,"""") If i > 0 Then Get_Token = Mid(wk,1,i-1) End If End Function Sub Putlog(data) logf.WriteLine data End Sub