Google Drive OAuth2 Tokens

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Google Drive OAuth2 Tokens

26 Feb 2020, 09:57

Have been working with AHK for a while, but currently have been researching oAuth2 with google drive for days and making a little or no progress.

Code: Select all

ClientId := "xxxxxxxxxxxxxx.apps.googleusercontent.com"
ClientSecret := "xxxxxxxxxxxxxxx"

RedirectUri := "urn:ietf:wg:oauth:2.0:oob"
scope := "https www.googleapis.com /auth/drive.file"
FirstUrl := "https accounts.google.com /o/oauth2/auth?redirect_uri="UriEncode(RedirectUri) "&response_type=code&client_id=" ClientId "&scope=" UriEncode(scope) "&clientSecret=" ClientSecret "&approval_prompt=force"

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate(FirstUrl)
while wb.ReadyState!=4  || wb.document.readyState!="complete" || wb.busy
	Sleep 100
	

	UriEncode(Uri)
{
	VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
	StrPut(Uri, &Var, "UTF-8")
	f := A_FormatInteger
	SetFormat, IntegerFast, H
	While Code := NumGet(Var, A_Index - 1, "UChar")
		If (Code >= 0x30 && Code <= 0x39 ; 0-9
         || Code >= 0x41 && Code <= 0x5A ; A-Z
         || Code >= 0x61 && Code <= 0x7A) ; a-z
			Res .= Chr(Code)
	Else
		Res .= "%" . SubStr(Code + 0x100, -1)
	SetFormat, IntegerFast, %f%
	Return, Res
}
At the end after approval of permissions via browser I receive an AuthorizationCode that use in AHK, this will get me access token and refresh token.

Code: Select all

ClientId := "xxxxxxxxxxx.apps.googleusercontent.com"
ClientSecret := "xxxxxxxxxxxxxx"
AuthorizationCode := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


RedirectUri := "urn:ietf:wg:oauth:2.0:oob"
Request := "grant_type=authorization_code&code=" AuthorizationCode "&redirect_uri=" UriEncode(RedirectUri) "&client_id=" ClientId "&client_secret=" ClientSecret
WinHTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
WinHTTP.Open("POST", "https accounts.google.com /o/oauth2/token",true)
WinHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
WinHTTP.Send(Request)
WinHTTP.WaitForResponse()
msgbox % clipboard := WinHTTP.ResponseText
Example of using access token: (note used CreateFormData function)

Code: Select all

access_token := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

CreateFormData(postData, hdr_ContentType, objParam)

WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHTTP.Open("POST", "https www.googleapis.com /upload/drive/v3/files",true)
WinHTTP.SetRequestHeader("Content-Type", hdr_ContentType)
WinHTTP.SetRequestHeader("Authorization", "Bearer " access_token)
WinHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
WinHTTP.Option(6) := False ; No auto redirect
WinHTTP.Send(postData)
WinHTTP.WaitForResponse()
MsgBox % WinHTTP.ResponseText
Example of obtaining access token from refresh token:

Code: Select all

ClientId := "xxxxxxxxxxxxxx7.apps.googleusercontent.com"
ClientSecret := "xxxxxxxxxxxxxxxxxxxxxxxxxx"
RefreshToken := "1//0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrXc"


Request := "refresh_token=" RefreshToken "&client_id=" ClientId "&client_secret=" ClientSecret "&grant_type=refresh_token"
WinHTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
WinHTTP.Open("POST", "https accounts.google.com /o/oauth2/token", true)
WinHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
WinHTTP.Send(Request)
WinHTTP.WaitForResponse()
msgbox % clipboard := WinHTTP.ResponseText
To my understanding I have to use refresh token to obtain access token, because access token expires after X minutes? refresh token expire after 6mo of not using it? Now I have to find how to parse WinHTTP response.

Thank you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: roeleboele and 374 guests