' ' gmail oauth2パラメータ取得スクリプト(gmail-oauth2.vbs) ' 2021.7.1 B21Soft, Inc. ' 2022.3.23 更新 ブラウザ選択機能追加 BTYPE="c" ' c:Chrome e:Edge i:IE ' redirect_uri=urn:ietf:wg:oauth:2.0:oob => redirect_uri=http://localhost/notarealpage ' 2022.8.26 更新 Chrome 32/64ビット版チェック ' ログに server= popserver= 定義追加 ''============== ★ご注意 修正後にSJIS で保存してください。 UTF-8 だとエラーになります。 '' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '' ログファイルセット LOGFILE="C:\wk\●●slog.txt" '' curl トレースファイルセット CURLTRACE1="--trace-ascii C:\wk\gmail\●●clog1.txt" CURLTRACE2="--trace-ascii C:\wk\gmail\●●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="c" ' c:Chrome e:Edge i:IE If BTYPE = "c" Then x86chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" if fso.FileExists(x86chrome) then objShell.Exec x86chrome & " -url " & ret Wscript.Quit End If x64chrome = "C:\Program Files\Google\Chrome\Application\chrome.exe" if fso.FileExists(x64chrome) then objShell.Exec x64chrome & " -url " & ret Wscript.Quit End If Wscript.echo "Chrome not found. use Edge." 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リフレッシュトークン取得フェーズ http://localhost/notarealpage redirect_uri=urn:ietf:wg:oauth:2.0:oob 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 "server=TLS smtp.gmail.com:587" Putlog "popserver=pop.gmail.com:995" 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