Popular Post Ascer Posted February 10, 2018 Popular Post Posted February 10, 2018 (edited) 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. Google account. oAuth.au3 Download 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! YouTube 4.2 Screenshots from authorization process (Polish language) Go to https://console.developers.google.com/apis/dashboard and accept current rules. Next create an new project Enter name of you new project and click Create Google will working now, please wait until finish. Next go to enable your API interface, we make if for Google Take "Gmail" in search input and after click in found result. Click Enable interface, Google will working now. Create your login credentials Select Windows Interface (combobox), User credentials (radio) and click button what is need bla bla Type name of a new client id for oAuth 2.0 and click Create a new Client ID. Next configure screen aplication, type some name and click Next. Google will working now. Last step on this website is download source with your credentials in *Json format. Now you received a file named client_id.json, it's how it look in Sublime Text: 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. Next Google ask you to permission for access to your personal details by application Autoit Now you can thing is something wrong but all is ok, you need to copy all after code= . It your access code. 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) Exit EndIf 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) Exit EndIf 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 If you followed all this above steps im sure that you received all informations required for coding your Google API (Gmail, Dropbox, YouTube, Calender etc. See next thread: [UDF] Gmail API - Email automation with AutoIt! Edited February 17, 2018 by Ascer masvil, nacerbaaziz, angrof and 3 others 5 1
n3wbie Posted July 16, 2018 Posted July 16, 2018 This is wat i was looking for it would be great if you can have this tutorial in english language(Images Part) and thanks For udf I Havnt Tried this! But will do it soon
PaulC Posted September 29, 2018 Posted September 29, 2018 oAuth2GetAuthorizationCode OK oAuth2GetAccessToken not work
dmob Posted September 30, 2018 Posted September 30, 2018 On 9/29/2018 at 8:18 PM, PaulC said: oAuth2GetAccessToken not work It actually does; have used this UDF successfully, works as expected. Post your code so we can help you debug... PaulC 1
PaulC Posted October 1, 2018 Posted October 1, 2018 (edited) I want an example to interact with gcp compute (insert/check status/ get info instances), can you help me? Edited October 1, 2018 by PaulC
PaulC Posted October 2, 2018 Posted October 2, 2018 On 1/10/2018 at 2:37 AM, dmob said: It actually does; have used this UDF successfully, works as expected. Post your code so we can help you debug... Can you help me an examples about GCE (google compute engine) instance?
dmob Posted October 6, 2018 Posted October 6, 2018 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.
x_bennY Posted November 15, 2018 Posted November 15, 2018 (edited) i'm receiving this error: "+++ Line: 93, Func: aAuthGetAccessToken() -> Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request." Edited November 15, 2018 by x_bennY
Flax Posted November 15, 2018 Posted November 15, 2018 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
argumentum Posted November 16, 2018 Posted November 16, 2018 (edited) 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 November 16, 2018 by argumentum Parsix 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
x_bennY Posted November 16, 2018 Posted November 16, 2018 that problem is now fixed but i'm getting some kind of transport message error and the email is not beeing sent, i don't know why, i already have tried all ports/ssl combinations and i just can't get it working.
argumentum Posted November 17, 2018 Posted November 17, 2018 On 11/15/2018 at 11:01 PM, x_bennY said: i'm getting some kind of transport message error Then you get some kind of answer =P ( be explicit ) I'm using @Ascer's code with the above tweak without problems. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
x_bennY Posted November 24, 2018 Posted November 24, 2018 (edited) 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! MessageId: ThreadId: 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 November 24, 2018 by x_bennY argumentum 1
argumentum Posted November 24, 2018 Posted November 24, 2018 21 minutes ago, x_bennY said: i can't get it working https://www.google.com/search?q=google+oauth+"mail+service+not+enabled" https://speaking.email/FAQ/65/gmail-service-errorshttps://github.com/FlowCrypt/flowcrypt-browser/issues/435https://groups.google.com/forum/#!topic/google-apps-manager/g73WtuU1NUE x_bennY 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
MCv Posted October 26, 2019 Posted October 26, 2019 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).
argumentum Posted October 26, 2019 Posted October 26, 2019 if you had a CGI on localhost port 80, it would call it via your default browser. But you should see it ( the URL ) in the default's browser address bar. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
NassauSky Posted October 30, 2020 Posted October 30, 2020 (edited) 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 November 4, 2020 by NassauSky
NassauSky Posted November 5, 2020 Posted November 5, 2020 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. argumentum 1
argumentum Posted April 29, 2022 Posted April 29, 2022 On 4/25/2022 at 11:04 AM, kut0 said: This UDF not work for me. Could you help me? https://www.autoitscript.com/forum/topic/192391-udf-google-oauth-20-with-autoit/?do=findComment&comment=1409771 ...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. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now