Jump to content

[UDF] Google oAuth 2.0 with AutoIt.

Recommended Posts

  • 4 months later...
  • 2 months later...
On 10/2/2018 at 5:50 PM, PaulC said:

Can you help me an examples about GCE (google compute engine) instance?

I use this UDF is for authentication to Google services. Please refer to Google documentation for examples for the particular service you require. 

Link to post
Share on other sites
  • 1 month later...
10 hours ago, x_bennY said:

i'm receiving this error: "+++ Line: 93, Func: aAuthGetAccessToken() -> Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request."

I get the same error, but you should see all info is outputed in Console after that error :) So baiscly it works it just cant convert json format to array :)

Link to post
Share on other sites
Google must changed *json respond for this request.

yes and no. The spaces is the problem. patch this:

Func _JsonValue($sString, $sStart, $sEnd, $iMode = 0, $bCase = False)
    $sString = StringStripWS($sString,8)
    $sStart = StringStripWS($sStart,8)
    $sEnd = StringStripWS($sEnd,8)

and that should take care of it.

Edited by argumentum
Link to post
Share on other sites
On 17/11/2018 at 6:13 PM, argumentum said:

Then you get some kind of answer    =P

( be explicit )

I'm using @Ascer's code with the above tweak without problems.

mysteriously that message disappeared, but i can't get it working, it says that the message has been sent but it's not, this is the console output:

Message Sent Sucessfully!
Sent message labels_ids:




Problem solved!

Several possible reasons:
1) Your trial period could have expired (if you use one). Check your status at admin.google.com
2) There is no email address that you use in FROM field created in your G Suite. So just create one.
3) Your email address is not a Gmail account. It is possible to be a Google Account but not a Gmail account (for example if you use the account for YouTube but have never enabled Gmail).
4) You may need to enable the Gmail feature or Gmail API access in your G Suite.

Edited by x_bennY
Link to post
Share on other sites
  • 11 months later...

Does this still work? I tried getting an access code, but executing this code doesn't give me an URL with a code=

#include <oAuth.au3>

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

oAuth2GetAuthorizationCode($sClientId, $sRedirectUri)

(of course I changed it with my own ClientId).

Link to post
Share on other sites
  • 1 year later...

Hi all,

Testing this now. It's hard to interpret the order of events

  •  Downloaded client_id.json  (OK, took me time to figure out how to do  that.)
  •  Got authorization after it opened up my default browser (after taking the client id & client secret from the downloaded client_id.json)
  •  The above authorization url included code=xxxxxxxxxx&scope=https://mail.google.com/ so  I did not use the scope in my authorization code.
  •  Next thing I tried was to ask Google about our Access Token and Refresh Token as shown in the instructions and I received the following result:
$oHttp.ResponseText = {
  "access_token": "xxxxxxxxxxx",
  "expires_in": 3599,
  "refresh_token": "1//0jHwqE this is a sample q8n5lgDCAK-",
  "scope": "https://mail.google.com/",
  "token_type": "Bearer"
+++ Something wrong with reading ResponseText.

Something wrong with response text but I did receive what seems to be a valid refresh token so i used that in a gmail sample and received the following:

$oHttp.ResponseText = {
  "access_token": "ya2 This is a sample kNY",
  "expires_in": 3599,
  "scope": "https://mail.google.com/",
  "token_type": "Bearer"
+++ Line: 34, Func: <none> -> Failed to get access token. Exiting..
>Exit code: 0    Time: 0.4095

So basically I'm stuck there at failing to get access token.  Not sure if it's the response text from the 1st code snippet I posted trying to obtain the access token or if someone how I should have some kind of code to keep obtaining the refresh token.  

Thanks for any ideas or tips.

Edited by NassauSky
Link to post
Share on other sites

Thanks @argumentum

Took me a bit of time to understand the whole workflow.  I was going to use my own method to extract the json values because I wasn't sure exactly where your code belonged. 

In other words looking inside the oAuth.au3 file there was a function called _JsonValue and I just had to add your code:

    $sString = StringStripWS($sString,8)
    $sStart = StringStripWS($sStart,8)
    $sEnd = StringStripWS($sEnd,8)

to the top of the function and leave the rest of the code below it in the function intact.

I also realized I reauthorized so many times playing with this method, I wound up just automating the whole process and then received some great tokens to use in the gmail function.  Next and last step is somehow figuring out how to create a single function to use the tokens and log into a Google sheet and extract the value of a single cell.  That's going to be the hardest part of the code :-/


Thanks again.

Link to post
Share on other sites
  • 1 year later...
On 4/25/2022 at 11:04 AM, kut0 said:

This UDF not work for me. Could you help me?


...not using this UDF myself but that was the solution back then. Also, paste the text instead of a pic. of the text as a rule of thumb.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By marko001
      Hi all,
      I'm not smart enough to clearly understand how to "translate this code into AutoIt call, so I used what I did in the past but it doesn't work properly.
      Basically I need to receive the Auth Token in order to place further calls.
      API docs on Paypal site says:
      curl -v -X POST "https://api-m.sandbox.paypal.com/v1/oauth2/token" \ -u "<CLIENT_ID>:<CLIENT_SECRET>" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials" or api-m.paypal.com if using live server.
      And this is my code (i just canceled the keys received from PP)
      #include <Array.au3> #include <string.au3> #include <MsgBoxConstants.au3> #Region Paypal Const Global $sURL = "https://api-m.paypal.com" ; /v1/oauth2/token - https://api-m.sandbox.paypal.com #EndRegion Paypal Const #Region keys Global $ClientID = "****" Global $SecretID = "****" #EndRegion keys _Test() Func _Test() Local $sResult = PayPalGet("/v1/oauth2/token") ConsoleWrite("$sResult: " & $sResult & @CRLF) EndFunc ;==>_Test Func PayPalGet($sMethod) Local $sJsonReq = '-u "' & $ClientID & ':' & $SecretID & '"' & _ '\ -H "Content-Type: application/x-www-form-urlencoded" \' & _ '-d "grant_type=client_credentials"' Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", $sURL & $sMethod, False) $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $oHTTP.Send(Binary($sJsonReq)) Local $sReceived = $oHTTP.ResponseText Return $sReceived Return $sReceived EndFunc ;==>PayPalGet When I run it I get:
      $sResult: {"error":"invalid_client","error_description":"Client Authentication failed"} Can you please help me to sort it out?
      Thanks in advance
    • By Danyfirex
      Hello guys.  I recently saw some posts that Windows 10 provides OCR API. So I decided to create a UDF.
      What's UWPOCR?
      UWPOCR UDF is a simple library to use Universal Windows Platform Optical character recognition API.
      Get Text From Image File. Get Text From GDI+ Bitmap. Easy to use.  Usage:
      #include "..\UWPOCR.au3" _Example() Func _Example() Local $sOCRTextResult = _UWPOCR_GetText(FileOpenDialog("Select Image", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.tif;*.gif)")) MsgBox(0,"",$sOCRTextResult) EndFunc Get Words Rect(Example):

      More examples here.
      Check UWPOCR UDF on GitHub.
    • By mLipok
      This is TeamViewer.au3 UDF for TeamViewer API.
      ; #INDEX# ======================================================================== ; Title .........: TeamViewer.au3 ; AutoIt Version : ; Language ......: English ; Description ...: A collection of function for use with TeamViewer API ; Author ........: mLipok ; Modified ......: ; URL ...........: ; URL ...........: https://www.teamviewer.com/ ; URL ...........: https://www.teamviewer.com/en/integrations/ ; URL ...........: https://integrate.teamviewer.com/en/develop/api/get-started/ ; URL ...........: https://downloadeu1.teamviewer.com/integrate/TeamViewer_API_Documentation.pdf ; Remarks .......: This UDF was created based on TeamViewer_API_Documentation.pdf v 1.4.1 ; Remarks .......: This UDF is using Free Chilkat component look here https://www.autoitscript.com/forum/files/file/433-chilkat-udf/ ; Remarks .......: Documentation is "work in progress" ; Date ..........: 2017/02/08 ; Version .......: 0.1.1 BETA - Work in progress ; ================================================================================ in TeamViewer_Example.au3 you can see few examples:

      Func _Example() ; If not exist then create new INI file from template If Not FileExists('TeamViewer_Example.ini') Then FileCopy('TeamViewer_Example — Template.ini', 'TeamViewer_Example.ini') ; Read Access Token from INI Local $sTV_AccessToken = IniRead('TeamViewer_Example.ini', 'Settings', 'AccessToken', '') If $sTV_AccessToken = '' Then ; Your Access Token, can be left empty when OAuth (below) is configured. ; ClientId = <----------------- Create an app in your TeamViewer Management Console and insert the client ID to the INI ; ClientSecret = <------------- Insert your client secret to the INI ; AuthorizationCode = <-------- Visit https://webapi.teamviewer.com/api/v1/oauth2/authorize?response_type=code&client_id=YOUR$i_ClientIdHERE ; Login, grant the permissions (popup) and put the code shown in the AuthorizationCode variable to the INI Local $sTVOAuth_ClientID = IniRead('TeamViewer_Example.ini', 'OAuth2', 'ClientID', '') Local $sTVOAuth_ClientSecret = IniRead('TeamViewer_Example.ini', 'OAuth2', 'ClientSecret', '') _IECreate('https://webapi.teamviewer.com/api/v1/oauth2/authorize?response_type=code&client_id=' & $sTVOAuth_ClientID) ; Local $sTVOAuth_AuthorizationCode = IniRead('TeamViewer_Example.ini', 'OAuth2', 'authorizationCode', '') Local $sTVOAuth_AuthorizationCode = InputBox('AuthorizationCode', 'Please provide TV OAuth2 AuthorizationCode') If @error Then Return If $sTVOAuth_ClientID Then $sTV_AccessToken = _TVAPI_RequestOAuth2_AccessToken($sTVOAuth_ClientID, $sTVOAuth_ClientSecret, $sTVOAuth_AuthorizationCode) EndIf If $sTV_AccessToken Then _TVAPI_AccessToken($sTV_AccessToken) If _TVAPI_Ping() = True Then ; ping API to check connection and $sTV_AccessToken _Example_TeamViewer__1_Devices_SaveToFile() ;~ _Example_TeamViewer__2_Devices_ChangeDetails() ;~ _Example_TeamViewer__3_Devices_GetDevicesSingleID() ;~ _Example_TeamViewer__4_Reports_GetAllConnections() ;~ _Example_TeamViewer__5_Users_GetUserInfomation() ;~ _Example_TeamViewer__6_Groups_ListGroups() ;~ _Example_TeamViewer__7_Devices_AddDeleteDevice() Else MsgBox(0, '_TVAPI_Ping', "$v_Token or connection problem.") EndIf EndFunc ;==>_Example You can download it here:
      I'm using TeamViewer_Example.ini to store my secret tokens/keys.
      [Settings] AccessToken= [OAuth2] ClientID= ClientSecret= authorizationCode=  
    • By nacerbaaziz
      hello guys, please i need your help
      am trying to work with CreateWindowEx api, i created the window with it controls, also i setup the call back function
      i'am using WinMSGLoop to focus with the keyboard.
      here i have a problem, i hope that you can help me.
      on the controls i used the UDF that comme with the autoit, such as _GUIButton_Create, _GUIListBox_Create....
      but i can't find a STATIC control UDF, for that i used this

      local $h_ssrvlbl = _WinAPI_CreateWindowEx(0, "STATIC", "الخادم", BitOr($WS_VISIBLE, $WS_CHILD, $WS_CLIPSIBLINGS, $WS_CLIPCHILDREN), 250, 10, 100, 20, $hWnd)
      as you can see here, there is an arabic text, so here is the problem, the arabic text isn't show normally, what is the problem here?
      also i have  an other question about keyboard focus, when i used WinMSGLoop, it worked, but if i press alt+tab to switch windows or focus an other window and return back to my window, the focus of control is kill.
      can any one help me to solve that please?
      my code will be as file here with the include files
      i hope can any one help me here
      thanks in advance
      speed Test win.zip
    • By rcmaehl
      Hi all, 

      Recently my work swapped from Cisco CTIOS to Finesse. This completely threw me off as I had been automating the Win32 application and I had never done IUIAutomation before. As such I've been messing around with the API and will be adding code as I figure it out. While I do have Supervisor access, I will likely not be adding functions for those features yet.
      Currently Available Functions:
      User API - Query and Set User Info
      Dialog API - Query and Set Call and other Dialog Info
      Queue API - Query Assigned Queues
      Team API - Query Users in a Team

      Support for this UDF can be obtained in my Discord Server
  • Create New...