Problem Getting Oath Token in response.



  • I have created code to retrieve an access token back using the XMLHttp Post Method. According to documentation I must post with my username and key in the body and add the header content type shown in the code. The code runs and gives me an error of {"OK":0,"response Code":400} Bad Request if I use the code as posted. If I comment out the body after the hReq.send and uncomment the user and key in the open string I get the 404 error.
    Sub IndexGetToken3()

    Dim hReq As Object, JSON As Object, i As Integer, item As Object
    Set hReq = CreateObject("MSXML2.XMLHTTP")
    Dim myurl As String
    Dim strUserName As String
    Dim strKey As String
    Dim key As String
    Dim username As String

    myurl = "https://auth.indexexchange.com/auth/oauth/token"
    strUserName = "XXXX"
    strKey = "XXXX"

    hReq.Open "POST", myurl, False ', strUserName, strKey
    hReq.setRequestHeader "Content-Type", "application/json; charset=utf-8"
    'hReq.setRequestHeader "Content-Type", "Application/x-www-form-urlencoded"
    'hReq.setRequestHeader "Authorization", "Basic" + strUserName + ":" + strPassword
    hReq.send ("{""username"":" & strUserName & ",""key"":" & strKey & "}")

    MsgBox hReq.responseText 'used for troubleshooting
    'Sheets("String Dump").Range("B2") = hReq.responseText

    End Sub

    Can anyone see the problem?


  • administrators

    @cturley You are missing quotes in username and key. This should work -

    hReq.Open "POST", myurl, False ', strUserName, strKey
    hReq.setRequestHeader "Content-Type", "application/json; charset=utf-8"
    hReq.send ("{""username"":" & """" & strUserName & """" & ",""key"":" & """" & strKey & """" & "}")
    MsgBox hReq.responseText
    


  • Great! That did the trick. Thank you so much.....


guest-login-reply
 

reconnecting-message