Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vba xmlhttprequest API oauth configuration 1

Status
Not open for further replies.

MrMode

Technical User
Aug 28, 2003
195
GB
I am able to connect to an api and collect data if it does not require authentication via xmlhttprequest within VBA module of an MS Access Database

Code:
sub ReadDataViaAPI()
Dim reader As New XMLHTTP60
    reader.Open "GET", "[URL unfurl="true"]https://api.github.com/orgs/dotnet/repos"[/URL]    
    reader.setRequestHeader "Accept", "application/json"
    reader.send
    
    Do Until reader.ReadyState = 4
        DoEvents
    Loop
    
    If reader.status = 200 Then
[indent][/indent]'do something here with exposed records(s)
    End If
end sub

However, where I am struggling is when I need to provide a consumer key and consumer secret as authentication is oAuth 1.0a. I do not know how to configure the setRequestHeader as most examples I have been able to find use basic authentication with username / password combinations. I am also not clear if that is the correct place to be trying to set the key and secret?

Code:
sub ReadDataViaAPI()
Dim reader As New XMLHTTP60
    reader.Open "GET", "[URL unfurl="true"]https://api.github.com/orgs/dotnet/repos"[/URL]    
    reader.setRequestHeader "Accept", "application/json"
reader.setRequestHeader "Authorization", 'how do I configure this element?
    reader.send
    
    Do Until reader.ReadyState = 4
        DoEvents
    Loop
    
    If reader.status = 200 Then
[indent][/indent]'do something here with exposed records(s)
    End If
end sub

Any suggestions are most welcome
 
The Authorization header is simply a Base64 encoding of the username and password joined with a colon:
Code:
reader.setRequestHeader "Authorization", EncodeBase64(strUserName & ":" & strOauthToken)

There are plenty of Base64 functions out there, but something like this would work
Code:
Function EncodeBase64(text As String) As String
  Dim arrData() As Byte
  arrData = StrConv(text, vbFromUnicode)

  Dim objXML As Variant
  Dim objNode As Variant

  Set objXML = CreateObject("MSXML2.DOMDocument")
  Set objNode = objXML.createElement("b64")

  objNode.dataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64 = objNode.text

  Set objNode = Nothing
  Set objXML = Nothing
End Function

If you're attempting to use the github API, you will need to set a user agent.


----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top