Newbie help with API interaction Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Newbie help with API interaction

17 May 2017, 19:19

Hi,

I'm a beginner at AHK. The most I can do right now is create GUIs, macro interact with web pages, declare and access variables from the clipboard, move windows, use If/Then, Loop and basics like that.

I was looking at these threads:
https://autohotkey.com/board/topic/1484 ... -api-call/
and
https://autohotkey.com/boards/viewtopic.php?t=17910

Because they seem to imply what I need to do... but I have NO idea what they are talking about.
I already have the API key I need, as well as the user name and password for the server....
... but that's as far as I can get. I don't even know where to begin.

What I want to do is have AHK authenticate with the server and access the API.
It then needs to look at this:
Request Method:GET
Status Code:200 OK

in the headers of the web element.

There's a lot more it needs to do after that... but one step at a time. First I just need to figure out how to get AHK to talk to the API... I tried to find information on "cURL" but every thread I find seems to expect you already know what cURL is and how to use it. Could someone help me get started on this?

Thank you!
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

18 May 2017, 10:41

Thanks! I'll take a look at this tonight and see if I can make it work!
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

23 May 2017, 18:05

Hmmm both these threads seem to expect you know what they are talking about. I tried to check the documentation for AHK and I couldn't find anything explaining winhttp or curl or examples of how to use it like you can for WinWait, WinMove and so on.

EDIT: I guess what I'm looking for is documentation about what winhttp is? I think? I'm not really sure where to start.
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

23 May 2017, 18:26

I think cURL Is what I need to use based on that first thread I linked (since it is specifically talking about API interaction). Can anyone help a TOTAL NON CODER figure this out? Please keep in mind the best I can do is move windows, store clip board as variables and use it to sendinput and very basic stuff like that.

EDIT: Oh I forgot to mention the API uses JSON so I'll be interacting with that to get data.
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

23 May 2017, 18:57

Okay, here's what I got so far from reading the forums but man, I'm winging it hard. @-@

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;					Contact API					;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MsgBox,0,Check Data, Token: %API_Token%			;Confirm the variables were stored, they are not being shown here for safety.
MsgBox,0,Check Data, Username: %API_USER%		;Confirm
MsgBox,0,Check Data, Password: %API_PASS%		;Confirm
MsgBox,0,Check Data, URL: %API_URL%				;Confirm web address where the API server is.

WinHTTP := ComObjCreate("WinHTTP.WinHttpRequest.5.1")			;Load the library????
WinHTTP.Open("GET",API_URL, 0)									;Access the API. What's the ",0" at the end?
WinHTTP.SetCredentials(API_Token,API_PASS, 0)					;SEE PROBLEM QUERY.


;;I don't understand the below.

WinHTTP.SetRequestHeader("Content-Type", "application/json")
Body = ""
Progress, 25
WinHTTP.Send(Body)
Result := WinHTTP.ResponseText
Status := WinHTTP.Status
Progress, Off

;;I don't understand the above.

IF Status = 200			;"GET" The HTTP page status???
{
  Msgbox,0,, OKAY		;Did it work?
}
else
{
 Msgbox,0,, FAILED		;Did it fail?
}
PROBLEM QUERY
This is the part that confuses me. I can get an OKAY response (in my "If status 200"). It's returning as 200 status okay... but I didn't declare the username. I declared the token and a password....but no where did I state the username. This server needs a username and password to log in... So... what happened?

EDIT: I don't think it's logging in, it's only checking to see if the page has loaded... I'm still not understanding how to actually send a login request.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Newbie help with API interaction

24 May 2017, 01:29

As you've done already you should go with a plain AHK related code if possible. That said, for those who struggle with AHK's COM/DOM & [JSON] abilities (or still use AHK v 1.0.x ?) [this] might be an option.

Good luck.
Last edited by BoBo on 24 May 2017, 01:39, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Newbie help with API interaction

24 May 2017, 01:38

DrW wrote:Okay, here's what I got so far from reading the forums but man, I'm winging it hard. @-@

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;					Contact API					;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MsgBox,0,Check Data, Token: %API_Token%			;Confirm the variables were stored, they are not being shown here for safety.
MsgBox,0,Check Data, Username: %API_USER%		;Confirm
MsgBox,0,Check Data, Password: %API_PASS%		;Confirm
MsgBox,0,Check Data, URL: %API_URL%				;Confirm web address where the API server is.

WinHTTP := ComObjCreate("WinHTTP.WinHttpRequest.5.1")			;Load the library????
WinHTTP.Open("GET",API_URL, 0)									;Access the API. What's the ",0" at the end?
WinHTTP.SetCredentials(API_Token,API_PASS, 0)					;SEE PROBLEM QUERY.


;;I don't understand the below.

WinHTTP.SetRequestHeader("Content-Type", "application/json")
Body = ""
Progress, 25
WinHTTP.Send(Body)
Result := WinHTTP.ResponseText
Status := WinHTTP.Status
Progress, Off

;;I don't understand the above.

IF Status = 200			;"GET" The HTTP page status???
{
  Msgbox,0,, OKAY		;Did it work?
}
else
{
 Msgbox,0,, FAILED		;Did it fail?
}
PROBLEM QUERY
This is the part that confuses me. I can get an OKAY response (in my "If status 200"). It's returning as 200 status okay... but I didn't declare the username. I declared the token and a password....but no where did I state the username. This server needs a username and password to log in... So... what happened?

EDIT: I don't think it's logging in, it's only checking to see if the page has loaded... I'm still not understanding how to actually send a login request.
Plain guessing, in case to handover login data isn't the server expecting to receive the request using the POST method instead of the GET method ???

[ WinHttp.open("POST",... ) ]. :)
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

24 May 2017, 11:51

BoBo wrote:As you've done already you should go with a plain AHK related code if possible. That said, for those who struggle with AHK's COM/DOM & [JSON] abilities (or still use AHK v 1.0.x ?) [this] might be an option.

Good luck.
So does this mean that the winhttp, COM and JSON are libraries already integrated with AHK and I can just call on them without having to use anything external (like other software or download anything extra)?

I'll try that with POST and see what I get. Man this is so complicated. >_< But I'm trying!
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction

25 May 2017, 18:24

Okay, so using this thread https://autohotkey.com/boards/viewtopic.php?t=92 I've made a bit of progress.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;					Contact API					;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;PostData := "username=Pulover&password=" pass "&login=Login" ;Original code. the API_USER and PASS are declared before this.
PostData := "username=" API_USER "&password=" API_PASS "&login=Login"

oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;Post request

oHTTP.Open("POST", API_URL , False)
;Add User-Agent header

oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")
;Add Referer header

oHTTP.SetRequestHeader("Referer", API_URL)
;Add Content-Type

oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
;Send POST request

oHTTP.Send(PostData)
;Get received data

Gui, Add, Edit, w800 r30, % oHTTP.ResponseText
Gui, Show
return

GuiClose:
ExitApp
Now this time when I use it I get the following error:
{"error":"Unauthorized API request, api_key header is missing"}

This seems like progress, so it's reaching the API now, but it's... missing the api_key header? I'm not sure what that means.

Help please?
DrW
Posts: 10
Joined: 17 May 2017, 19:08

Re: Newbie help with API interaction  Topic is solved

26 May 2017, 17:20

I got it working.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;					Contact API					;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Variable that holds the winhttp commands.
COMMAND := ComObjCreate("WinHttp.WinHttpRequest.5.1")

;Add User-Agent header
COMMAND.Open("POST", API_URL , FALSE,0) ;<- Access the API back end.

;Contact the API back end.
COMMAND.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36") ;<- Method to emulate for accessing the API.
COMMAND.SetRequestHeader("Referer", API_URL)
COMMAND.SetRequestHeader("Content-Type", "application/json") ;<- What you're talking to. JSON in this case.
COMMAND.SetRequestHeader("api_key", API_KEY ) ;<- your access key.

;Log In
COMMAND.Send(API_Login)

;For testing purposes. Did it log in?
Gui, Add, Edit, w800 r30, % COMMAND.ResponseText
Gui, Show
return

GuiClose:
ExitApp
Turns out I had the API Token but not the API access key. So I declared the API access key and changed the "api_key" line 27 to what I have now. I now have successful contact.
Note that all the variables are being declared earlier but are not being posted here for security reasons.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Joey5 and 241 guests