VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.79k stars 573 forks source link

Authentication? #218

Open L-hoss opened 2 years ago

L-hoss commented 2 years ago

I'm new to github, I suppose this is the place to ask questions? I've only had minor exposure to json so far. Does this code include whatever would be needed to pull data from an api that requires authentication? I searched here and looked and some code samples and couldn't determine this.

aholden10 commented 2 years ago

Not as far as I know...authentication is done separately.  You would kick VBA-JSON into action once you have obtained your JSON payloads and want to use the data, but it can't get the data for you.

On Friday, December 17, 2021, 09:32:22 PM EST, L-hoss ***@***.***> wrote:  

I'm new to github, I suppose this is the place to ask questions? I've only had minor exposure to json so far. Does this code include whatever would be needed to pull data from an api that requires authentication? I searched here and looked and some code samples and couldn't determine this.

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.***>

L-hoss commented 2 years ago

Yeah! Thanks for that...I've been experimenting with vba-json since I wrote and I discovered what you suggest. The example I'm using make the request via WinHttp.WinHttpRequest.5.1, and for non secured feeds, it works a treat. If I try the SetCredentials method I've been getting "Request is missing Authorization header" and I'm trying to sort that out now. If anyone has a clue please let me know!

aholden10 commented 2 years ago

My experience with API calls is limited - with one commercial app I use there are tons of available libraries on Github and it is best to use them if they exist.

For APIs that are a bit simpler but still need a password, the APIs I use require tokens and the API author is (hopefully!) going to document how to obtain those tokens, and then how to send the authentication tokens in the request. I've never had to use the SetCredentials method, it appears you would substitute that for a couple of my lines just before objHTTP.Send. You should find tons of examples on different sites if you search for the SetRequestHeader keyword. Here's a snippet: Dim objHTTP As New WinHttp.WinHttpRequest        'set WinHTTP reference in toolsDim info As StringDim tokenstring As StringDim API_key As StringDim URL as string 'in my case tokenstring is the temporary session token, APIkey is the permanent user keytokenstring = "session123"API_key = "xyz"URL = "https://whatever.org/API/"        'note that some APIs require a percent encoded URL so you might have to run this through a URL encoder 'in the headers below items within quotes are fixed names that the API owner should give you. Variables are values that the user suppliesobjHTTP.Open "GET", URL, FalseobjHTTP.SetRequestHeader "Authorization", "Bearer " & tokenstringobjHTTP.SetRequestHeader "Named-APIkey", API_keyobjHTTP.Send 'I often like to print all the responses to the immediate windowDebug.Print objHTTP.StatusDebug.Print objHTTP.ResponseTextDebug.Print objHTTP.GetAllResponseHeaders 'And this string should contain the JSON payloadinfo = objHTTP.ResponseText Hope it helps!

On Friday, December 17, 2021, 11:00:01 PM EST, L-hoss ***@***.***> wrote:  

Yeah! Thanks for that...I've been experimenting with vba-json since I wrote and I discovered what you suggest. The example I'm using make the request via WinHttp.WinHttpRequest.5.1, and for non secured feeds, it works a treat. If I try the SetCredentials method I've been getting "Request is missing Authorization header" and I'm trying to sort that out now. If anyone has a clue please let me know!

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>