I try to create an object of type MSXML2.XMLHTTP.6.0 but when i call the method Open I get an error :
Object doesn't support this property or method.
Here is my code:
set objXmlHttp = server.CreateObject("MSXML2.XMLHTTP.6.0")
objXmlHttp.Open "POST", strURL , false
I have tried using the TLS 1.2 but the same thing happens.
The strURL variable receives an address with HTTPS.
Any suggestions?
Related
HI All I am new in handling http requests and am using UFT and vbscript to achieve the below requirement..I am able to send a GET request in Postman and it has a pre-request script which contains below,
pm.environment.set("hmacCreationTime", new Date().getTime());
and on sending the Get request everytime we get a unique Auth token. Here the value from the pre-request script is passed as a request header.When i try to send get request from UFT(VB script) the request throws "400 bad status" but is working fine in postman with the request headers as below
so i hardcoded the header("timestamp") using setRequestHeader method in my uft script and now i am able to generate the auth token.Please find below code
strWebServiceURL = "https://demo.com/customer/account/v1/auth/getauthtoken"
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
oWinHttp.SetTimeouts 0, 360000, 360000, 360000
'Open a HTTP connection to a HTTP resource
oWinHttp.Open "GET", strWebServiceURL, False
'owin
oWinHttp.SetRequestHeader "timestamp","1629371122124"
oWinHttp.SetRequestHeader "clientId","clientId"
oWinHttp.SetRequestHeader "User-Agent","neoload"
'oWinHttp.
'Send a HTTP request to the HTTP server with the header and body info
oWinHttp.Send
oWinHttp.WaitForResponse
'Get response
getRestRequest = oWinHttp.ResponseText
Set oWinHttp = Nothing
So i guess the timestamp value from the request Headers are required for the GET request to run successfully and is dynamically fetched from the pre-request script.is there a way to fetch the Request header values from the script ,also i tried getAllresponseheaders(but the timestamp header is not fetched )from UFT script or is there any workaround to achieve this or a way to create the timestamp value in vbscript? Any help to this would be really helpful.Thanks in advance
The following worked fine and can be included in library functions (let me know how it goes for you):
strURLUNPW = "YourWServicesURL,WSUN,WSPW"
msgbox (WebService_GET("YourWServicesURL,WSUN,WSPW")
Public Function WebService_GET(strURLUNPW)
arrURLUNPW = Split(strURLUNPW,",")
strPW = arrURLUNPW(2)
strUN = arrURLUNPW(1)
strURL = arrURLUNPW(0)
WebService_GET = ""
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttpMain.open "GET",strURL,False,strUN,strPW
objXmlHttpMain.setRequestHeader "Accept", "*/*"
objXmlHttpMain.send strJSONToSend
WebService_GET = objXmlHttpMain.responseText
End Function
I am using Classic ASP and trying to use the JustGiving API.
I'd like to use it to display the total amount raised, and total donations received on my donation page, on my website.
I can see that info is available via:
https://api.justgiving.com/docs/resources/v1/Account/Retrieve
<%
vurl = "http://api.justgiving.com/---myIDhere---/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False
http.Send
Set dom = Server.CreateObject("msxml2.DOMDocument")
dom.loadXML http.responseText
Set items = dom.getElementsByTagName("account")
For Each item In items
Set var_totalDonated = item.getElementsByTagName("totalDonated")
If NOT (var_totalDonated IS Nothing) Then
var_totalDonated = ap(totalDonated(0).Text)
response.write var_totalDonated
End If
Next
%>
However, the page times out when I access it.
I think it's because I need to provide some authentication information as detailed here:
https://api.justgiving.com/docs/usage#protectedResources
So I got that authentication info.
But I have no idea how to "send" it to the API, so that it can authenticate me as a user and provide the info.
It also mentions providing info on the header via the link above this one (I can't post the link as I don't have enough reputation), but replace #protectedResources at the end of the URL with #contentTypes.
I'm sorry - am I also missing something on that side?
I'm sorry if I'm asking silly questions, but the info on the API docs assumes some level of intelligence on the part of the user, and I don't have a lot of it!
Any advice much appreciated.
Thanks
Thanks to John for your reply.
Based on that, I changed the code to:
<%
vurl = "https://api.justgiving.com/API_KEY/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False, "username", "pwd"
http.setTimeouts 5000, 5000, 10000, 10000 ''ms - resolve, connect, send, receive
http.setRequestHeader "Authorization", "Basic MY_AUTH_STRING"
http.Send
Set dom = Server.CreateObject("msxml2.DOMDocument")
dom.loadXML http.responseText
Set items = dom.getElementsByTagName("account")
For Each item In items
Set var_totalDonated = item.getElementsByTagName("totalDonated")
If NOT (var_totalDonated IS Nothing) Then
var_totalDonated = (var_totalDonated(0).Text)
response.write var_totalDonated
End If
Next
%>
But unfortunately the page still times out.
I'm checking also via:
groups.google.com/forum/#!topic/justgiving-api/Xhz5Fkxuy1s
But no answer so far.
Thanks again
Fixed Version
<%
Sub debug( varName )
Dim varValue
varValue = Eval( varName )
response.write "<p style='margin:10px; border-bottom:2px solid #ccc;border-top:1px solid #eaeaea;background-color:white;padding:10px;color:red;text-align:left;'><strong>" & varName & "</strong>: " & varvalue & "</p>" & vbcrlf & vbcrlf
End Sub
vurl = "https://api.justgiving.com/AP_KEY/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "GET", vurl, False, username, password
http.setTimeouts 5000, 5000, 10000, 10000 'ms - resolve, connect, send, receive
http.setRequestHeader "Authorization", "Basic AUTH_STRING"
http.Send
Response.ContentType = "application/xml"
Set dom = Server.CreateObject("msxml2.DOMDocument")
dom.loadXML http.responseText
Set items = dom.getElementsByTagName("account")
For Each item In items
Set var_totalDonated = item.getElementsByTagName("totalDonated")
If NOT (var_totalDonated IS Nothing) Then
var_totalDonated = ap(var_totalDonated(0).Text)
debug "var_totalDonated"
End If
Set var_totalRaised = item.getElementsByTagName("totalRaised")
If NOT (var_totalRaised IS Nothing) Then
var_totalRaised = ap(var_totalRaised(0).Text)
debug "var_totalRaised"
End If
Set var_totalGiftAid = item.getElementsByTagName("totalGiftAid")
If NOT (var_totalGiftAid IS Nothing) Then
var_totalGiftAid = ap(var_totalGiftAid(0).Text)
debug "var_totalGiftAid"
End If
Next
%>
Previously I was using:
vurl = "https://api.justgiving.com/AP_KEY/v1/account"
But when I changed it to https it worked.
I thought I had tried that previously, but obviously not.
Thanks again to John, I really appreciate your help!
Try
http.Open "GET", vurl, False, "yourusername", "yourpassword"
I don't know if this works on justgiving, but it does with the Bing API
Also, this question may be relevant
XmlHttp Request Basic Authentication Issue
Edit - using Response.ContentType and Msxml2.ServerXMLHTTP.6.0
vurl = "https://api.justgiving.com/API_KEY/v1/account"
Set http = Server.CreateObject("msxml2.ServerXMLHTTP.6.0")
http.Open "GET", vurl, False, "username", "pwd"
http.setTimeouts 5000, 5000, 10000, 10000 'ms - resolve, connect, send, receive'
http.setRequestHeader "Authorization", "Basic MY_AUTH_STRING"
http.Send
Response.ContentType = "application/xml"
Set items = http.responseXML.getElementsByTagName("account")
etc
Apologies for adding to an old post. However this has consistently come up when Googling for help on MailChimp API V3.0 and VBA.
This is the fix I used:
ret = objhttp.Open("POST", sURL, False)
objhttp.setRequestHeader "Content-Type", "application/json"
objhttp.setRequestHeader "Accept", "application/json"
'V3 API uses HTTP Basic Authorisation inside an https: wrapper.
'The standard windows method does not seem to work however the
'following hack does.
'In summary the user name and APIkey are seperated with a Colon: and
'base 64 encoded and added to a Http RequestHeader
objhttp.setRequestHeader "Authorization", "Basic " & Base64Encode(APIUser & ":" & ApiKey)
objhttp.send (sJson)
You will need to code the Base64Encode function. I grabbed some code from http://pastie.org/1192157 (Ex StackOverflow) and pasted it into a VBA Module.
Hope it helps.
I've been using XMLHTTP for making HTTP POST requests (From a VB5 application) to a WCF (self hosted in a windows service) endpoint with the next code and everything was working as expected,
Dim xmlhttp As MSXML2.XMLHTTP30
Dim blnSuccess As Boolean
Dim resp, strTit, strRes As String
Dim intPos1, intPos2, intPos3 As Integer
Dim mType As VbMsgBoxStyle
Set xmlhttp = New MSXML2.XMLHTTP30
xmlhttp.Open "POST", strURL, False
xmlhttp.setRequestHeader "Man", "POST " & strURL & " HTTP/1.1"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", strSOAPAction
Call xmlhttp.send(strSoap)
However since I was requested to change from HTTP to HTTPS Had to change WCF to add and SSL certificate by binding the certificate to the port, and everything was working fine while doing requests from postman, but the problem was that when I tried to test it on my VB application It did not work and I was prompted with this error run time error -214669728 (800c0008) The download of the specified resource has failed. So while doing some research I change my code to use SeverXMLHTTP instead of XMLHTTP and setOption for bypassing certificate errors as is shown in the next code
Dim xmlhttp As MSXML2.ServerXMLHTTP30
Dim blnSuccess As Boolean
Dim resp, strTit, strRes As String
Dim intPos1, intPos2, intPos3 As Integer
Dim mType As VbMsgBoxStyle
Set xmlhttp = New MSXML2.ServerXMLHTTP30
xmlhttp.Open "POST", strURL, False
xmlhttp.setRequestHeader "Man", "POST " & strURL & " HTTP/1.1"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", strSOAPAction
xmlhttp.setOption 2, 13056
blnSuccess = False
Call xmlhttp.send(strSoap)
The problem is that I been reading that as the name says, SeverXMLHTTP must be used for server application not client as my VB is acting in this context. I referred to this post
I am concerned if this is the correct path to follow but while reading this article
I think I will not have problems even this object points to use between server to server communication.
Could anybody instruct me a little bit about this, what I observed in testing everything is good so far, but I not quite experienced using this object .
Thanks
I am getting an empty response Microsoft Graph API although the status of request is 200.
I have been following this documentation. - https://learn.microsoft.com/en-us/graph/auth-v2-user
I successfully registered an app and got the ClientID.
I am using VBScript to create a GET Request - here is my code:
Dim oXMLHTTP
Dim oStream
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=######&response_type=code&response_mode=query&scope=offline_access+mail.read", False
oXMLHTTP.Send
wscript.echo oXMLHTTP.status
wscript.echo oXMLHTTP.responseBody
Can anyone advise on what I am doing wrong?
oXMLHTTP.status = 200
The process of using oauth code flow to obtain tokens and call api is as follows:
Enter this link in the browser to obtain the authorization code:
https://login.microsoftonline.com/{tenant id}/oauth2/authorize?
client_id={your-client-id}
&response_type=code
&redirect_uri=https://localhost:4500/web/completeoauth/ms
&response_mode=query
&scope=openid offline_access https://graph.microsoft.com/mail.read
&state=12345
Use code to get access token: here.
For your question, it is recommended that you use MSAL.NET for authentication, which eliminates the step of obtaining code.
Private Function Login() As Boolean
Dim publicClientApp As IPublicClientApplication
publicClientApp = PublicClientApplicationBuilder.Create(client_id).WithAuthority(authority).Build()
Dim accounts As IEnumerable(Of IAccount) = publicClientApp.GetAccountsAsync().Result()
Dim firstAccount As IAccount = accounts.FirstOrDefault()
Dim authResult As AuthenticationResult
Try
authResult = publicClientApp.AcquireTokenSilent(scopes, firstAccount).ExecuteAsync().Result()
Catch e As MsalUiRequiredException
Try
authResult = publicClientApp.AcquireTokenInteractive(scopes).ExecuteAsync().Result()
Catch ex As Exception
'user cancelled
Return False
End Try
Catch ex As Exception
Console.WriteLine($"Auth Exception: {ex.Message}")
Return False
End Try
_accessToken = authResult.AccessToken
Return True
End Function
Please see:VB.NET – USE MSAL.NET IN A CONSOLE APPLICATION TO AUTHENTICATE TO AZURE
Am trying to post a request and get a response through VBS.
Dim response
Set xHttp = CreateObject("Microsoft.XMLHTTP")
xHttp.Open "POST", "https://idoc-api-int.platform-test.sample.com/interactiveDocumentOrchestrator", False, u1i, p1i
xHttp.Send
response = xHttp.responseText
Msgbox response
Basically, I'm using SOAP UI to send a request to the URL with a body content and I will be getting the response. I'm trying to achieve it through a VBScript. Kindly suggest if there is any way I am able to do this.