• Become a Power user Intro to AutoHotkey Intermediate AutoHotkey Intermediate Objects GUIs are Easy w/AutoHotkey Painlessly switch from V1 to V2

Example Webservice / API call- Yelp and working through oAuth2

In our API webinar we discussed how oAuth2 works however there is no better way to explain it than to actually work through an example!  In the below video & code I demonstrate how to access Yelp to perform the “handshake” with oAuth1 and receive the oAuth2 authentication.  I also demonstrate using a named Regular Expression to isolate the returned token.

;~  https://www.yelp.com/developers/documentation/v3/authentication
ID:="94MAk4OrJa_y7ww5IV7HQg"
Secret:="eaHaNtMp4SbdBLB7aWpReLhe2oGWqeT32cMDG8ksMUc6r4ARA2RmPo7o6y6mG0pS"

EndPoint:="https://api.yelp.com/oauth2/token"
QueryString:=QueryString_Builder({"grant_type":"client_credentials","client_id":ID,"client_secret":Secret})
;***********API call to get Authorization Token*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("POST",EndPoint . QueryString)
HTTP.Send()
data:=HTTP.ResponseText ;~ MsgBox,,title, % response
RegExMatch(data,".*access_token\x22:\s\x22(?<Token>.*?)\x22.*",Yelp_) ;RegEx to grab token
;~  MsgBox % Yelp_token

;~  https://www.yelp.com/developers/v3/manage_app?app_created=True
;~  https://www.yelp.com/developers/documentation/v3/business_search
EndPoint:="https://api.yelp.com/v3/businesses/search"

;~  https://www.yelp.com/developers/documentation/v2/all_category_list
;~  https://www.yelp.com/developers/documentation/v2/all_category_list/categories.json
QueryString:=QueryString_Builder({"term":"restaurants","location":"Coppell, TX","categories":"chicken_wings"})

;***********API call to yelp with Credentials*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", EndPoint . queryString)
HTTP.SetRequestHeader("Authorization","Bearer " . Yelp_token)
HTTP.Send()
Response_Data:=HTTP.ResponseText ;~ MsgBox,,title, % response
Response_Data:=formatjson(Response_Data)
SciTE_Output(Response_Data) ;Text,Clear=1,LineBreak=1,Exit=0


;***********QueryString Builders*******************
QueryString_Builder(kvp){
for key, value in kvp
  queryString.=((A_Index="1")?(url "?"):("&")) key "=" value
return queryString
}

Video walking through the API example / oAuth2 process

 

 

 

Example API Call- Top Reddit post to AutoHotkey subreddit

Top Reddit post

As we mentioned in our webservice / API webinar most APIs require Authentication.   In this example I noticed the number of records being returned by the API call was not nearly as many as when I did the same call with my browser  (I think this is because I didn’t bother to use my secret/token for oAuth2.)

I decided to use the developer tools in Chrome to see what headers were being sent to reddit.   Once I included the cookie header (data borrowed from my browser) I was returning a ton of data!  I also demonstrated the use of sXML_Pretty function by VxE to re-structure the XML to be “pretty” for humans.

If you’re new to API calls, be sure to get our API Syntax Writer



Here is the video where I document the Example API Call and show how to use the Chrome Developer tools