Jump to content

[UDF] Google oAuth 2.0 with AutoIt.

Recommended Posts


1. Description.

  • oAuth 2.0 is security system implemented by Google a few years ago.
  • You are able to connect into your Google accounts and manage documents.
  • In this UDF i show you how to pass first authorization process., this allow you to automate most of functions using API interface.

2. Requirements.

3. Possibilities

; Date:             2018-02-10, 14:21
; Description:      UDF for authorize your app with oAuth 2.0 Google.
; Function(s):
;                   oAuth2GetAuthorizationCode() -> Get Code for "grant".
;                   oAuth2GetAccessToken()       -> Get "access_token" and "refresh_token" first time.
;                   oAuth2RefreshAccessToken()   -> Get current "access_token" using "refresh_token".
; Author(s):        Ascer

4. Enable your Google API.

    4.1. Video Tutorial not mine!

    4.2 Screenshots from authorization process (Polish language) 


autoit 1.png


  • Next create an new project


autoit 2.png


  • Enter name of you new project and click Create


autoit 3.png


  • Google will working now, please wait until finish. Next go to enable your API interface, we make if for Google


autoit 4.png


  • Take "Gmail" in search input and after click in found result.


autoit 5.png


  • Click Enable interface, Google will working now.


autoit 6.png


  • Create your login credentials


autoit 7.png


  • Select Windows Interface (combobox), User credentials (radio) and click button what is need bla bla


autoit 8.png


  • Type name of a new client id for oAuth 2.0 and click Create a new Client ID.


autoit 9.png


  • Next configure screen aplication, type some name and click Next. Google will working now.


autoit 10.png


  • Last step on this website is download source with your credentials in *Json format.


autoit 11.png


  • Now you received a file named client_id.json, it's how it look in Sublime Text:


autoit 12.png


5. Coding.

  • Now we need to call a some function to get access code.


#include <oAuth.au3>

Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com" 
Local $sRedirectUri = "http://localhost"

oAuth2GetAuthorizationCode($sClientId, $sRedirectUri)


  • Function will execute default browser for ask you to permission.


autoit 13.png


  • Next Google ask you to permission for access to your personal details by application Autoit 


autoit 14.png


  • Now you can thing is something wrong but all is ok, you need to copy all after  code= . It your access code.


autoit 15.png


  • Let's now ask Google about our Access Token and Refresh Token


#include <oAuth.au3>

Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com"
Local $sClientSecret = "cWalvFr3WxiE6cjUkdmKEPo8"
Local $sAuthorizationCode = "4/AAAPXJOZ-Tz0s6mrx7JbV6nthXSfcxaszFh_aH0azVqHkSHkfiwE8uamcabn4eMbEWg1eAuUw7AU0PQ0XeWUFRo#"
Local $sRedirectUri = "http://localhost"

Local $aRet = oAuth2GetAccessToken($sClientId, $sClientSecret, $sAuthorizationCode, $sRedirectUri)

If Ubound($aRet) <> 4 then
    ConsoleWrite("+++ Something wrong with reading ResponseText." & @CRLF)

ConsoleWrite("Successfully received data from Google." & @CRLF)

ConsoleWrite("access_token: " & $aRet[0] & @CRLF)
ConsoleWrite("expires_in: " & $aRet[1] & @CRLF)
ConsoleWrite("refresh_token: " & $aRet[2] & @CRLF)
ConsoleWrite("token_type: " & $aRet[3] & @CRLF)


  • Important! When you received error 400 and output says: Invalid grant it means that your previous generated access_code lost validity and you need to generate new calling previus code.
  • When everything is fine you should received a 4 informations about your: access_token, expires_in, refresh_token and token_type.
  • Access_Token time is a little short so you need to know fuction possible to refresh it (tell Google that he should generate a new Token for you)


#include <oAuth.au3>

Local $sRefreshToken = "1/ba8JpW7TjQH3-UI1BvPaXhSf-oTQ4BmZAbBfhcKgKfY"
Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com"
Local $sClientSecret = "cWalvFr3WxiE6cjUkdmKEPo8"
Local $sRedirectUri = "http://localhost"

Local $aRet = oAuth2RefreshAccessToken($sRefreshToken, $sClientId, $sClientSecret)

If Ubound($aRet) <> 3 then
    ConsoleWrite("+++ Something wrong with reading ResponseText." & @CRLF)

ConsoleWrite("Successfully received data from Google." & @CRLF)

ConsoleWrite("access_token: " & $aRet[0] & @CRLF)
ConsoleWrite("expires_in: " & $aRet[1] & @CRLF)
ConsoleWrite("token_type: " & $aRet[2] & @CRLF)


6. Finish words

Edited by Ascer
  • Like 3
  • Thanks 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Iznogoud
      By Iznogoud
      I was wondering if someone can help me to create an AutoIT script which can create a ticket in a TopDesk environment.
      The API is wel documented, but i have not enough knowlegde at this moment to get this up from the ground. If someone could help me or if i could hire / pay someone to help me with this, that would be great.
      Information about the API can be found here: https://developers.topdesk.com/
      Is there a place where i maybe can hire AutoIT professionals to help me with this?
      On freelancer.com is an option, but there are alot of people responding which doesn't know a thing about AutoIT.
    • Ascer
      By Ascer
      1. Description.
      Automate communication with Gmail API using oAuth 2.0 security. 2. Requirements.
      Google Gmail account. Finished Authorization process. Look here 3. Possibilities.
      ;======================================================================================================================== ; Date: 2018-02-12, 11:46 ; ; Bug Fixs: 2018-02-17, 7:31 -> Fixed problems with adding items to array and minor bugs. ; ; Description: UDF for using Gmail API interface. This UDF requires oAuth.au3 and Gmail account. ; ; Function(s): ; gmailUsersGetProfile() -> Information about your account. ; gmailUsersLabelsList() -> Get all available labels ids. ex. "INBOX", "UNREAD" ; gmailUsersLabelsGet() -> Get information about specific label id. ; gmailUsersMessagesBatchDelete() -> Delete many messages emails by id. ; gmailUsersMessagesBatchModify() -> Set status for many messages ex. "INBOX", "UNREAD" ; gmailUsersMessagesDelete() -> Totaly delete email from ur account. ; gmailUsersMessagesGet() -> Get all information about specific email. ; gmailUsersMessagesList() -> Get list of last ~100 emails. ; gmailUsersMessagesModify() -> Modify single message. ; gmailUsersMessagesTrash() -> Put email in trash. ; gmailUsersMessagesUntrash() -> Restore email from trash. ; gmailUsersMessagesSend() -> Send email to single or group recipients. ; gmailUsersMessagesAttachmentsGet() -> Download attachment by id. ; ; Author(s): Ascer ;======================================================================================================================== 4. Downloads.
      oAuth.au3 Gmail API.au3 5. Examples.
      Sending emails
    • dadalt95
      By dadalt95
      Perform a simple google search!
      The script below works fine until fill the google form!
      What I can't find is how to submit the form, tried a couple of ways and none of them worked.

      #include <IE.au3> $oIE = _IECreate ("www.google.com") $o_form = _IEFormGetObjByName ($oIE, "f") $o_login = _IEFormElementGetObjByName ($o_form, "q") $username = "80251369" _IEFormElementSetValue ($o_login, $username) $o_numer = _IEGetObjByName($o_form, "btnK") _IEAction ($o_numer, "click")  
      The code runs without any problem.
      I don't know how to proceed!
      Thanks in advance!

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.