Jump to content

HTTP POST Request, Dell Warranty API


Recommended Posts

Hello,

I was approved for a v5 Dell TechDirect API key and am having some issues that I was hoping to receive some input on. My code is designed to retrieve warranty information through Dell's API for computers in our inventory as a part of our imaging process. Our existing code for their v4 API is working perfectly, but they recently made some changes to their process. Now, we are given an API key and a client secret, and we must use those to generate a token which expires every hour. I'm having what I consider to be an odd issue which makes the script, as we have historically used it, extremely problematic.

First, the code itself (note that there's obviously more code than this, but this is enough to demonstrate the issue):

;First we need to get the token
   ;Create HTTP Object
   Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")

   $oHttp.Option(9) = 0x800
   ;Open a POST request at the api. The 'False' is because we are opening the request synchronously
   $oHTTP.Open("POST", "https://apigtwb2c.us.dell.com/auth/oauth/v2/token", False)

   ;We need to set the content-type because our credentials are being sent in like a form
   $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

   ;In case the client_id or the client secret changes, here is where you would change them
   Local $client_id = "myapikey"
   Local $client_secret = "myclientsecret"

   ;Lastly we need to send the request, the string parameter is the credentials we are sending in.
   $oHTTP.Send('grant_type=client_credentials&client_id=' & $client_id & '&client_secret=' & $client_secret)

When the code was first written, it worked fine on the computer it was initially run on. Once the code was being tested by someone else on their computer, they received the following error:

"No credentials were available in the client certificate"

Upon reading into the issue, it seems that winhttp objects use the first cert in the "CURRENT_USER\My" store for some purpose unknown to us. The error itself seemed to be resolved by adding the line $oHttp.SetClientCertificate("CURRENT_USER\My\mycert") and pointing to a specific cert in the store (in our case, replace the "mycert" signature with the user's email address). However, we typically run this script as a local user account, rather than under a user's domain account, as a part of our computer imaging process. We are unable to make this script work under a local user account, and we aren't sure why pointing to a cert is even required. It doesn't seem to me that it should be.

Is there anyone out there with more knowledge that can point us in the right direction to make this work under a local user account?

Edit: FYI, I have already reached out to Dell and they just said they can't help me.

Edited by chaoticyeshua
Link to comment
Share on other sites

Try to use HTTP.au3

Or post what com error handler say to you.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 1 month later...
  • 2 years later...

Not pretty, but you can create a functional warranty lookup from it.

 

#include <HTTP.au3>
$url = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"
$postdata = "client_id=" & "YOURID" & "&" & "client_secret=" & "YOURSECRET" & "&" & "grant_type=client_credentials"
$test = _HTTP_Post1($url, $postdata)
ConsoleWrite($test)   ;this consolewrite will show you your new token if its older than an hour

$token ="3997b2f1-1781-426d-bc1f-bd7e0bd9011f"    ;will need to be updated with latest token
$ans = _http_get1("https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags=YOUR SERIAL")
MsgBox(0,"",$ans)

Func _HTTP_Post1($url, $postdata = '')
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", $url, False)
    If @error Then Return SetError(1, 0, 0)

    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    $oHTTP.Send($postdata)
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Post

Func _HTTP_Get1($url)
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    Local $res = $oHTTP.Open("GET", $url, False)
    If @error Then Return SetError(1, 0, 0)
    #forceref $res
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.SetRequestHeader("Authorization", "Bearer " &$token)

    $oHTTP.Send()
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Get

 

Edited by Jos
added codebox... please use <> next time.
Link to comment
Share on other sites

  • 2 years later...

Thank you.

I have downloaded it and I have added my own client-ID and the client-secret, but I get some errors.

We intercepted a COM Error !
Number is: 80020009
Windescription is: Exception occurred.
0We intercepted a COM Error !
Number is: 80020009
Windescription is: Exception occurred.

Yours sincerely

Kenneth.

Link to comment
Share on other sites

14 minutes ago, Valnurat said:

I have downloaded it and I have added my own client-ID and the client-secret, but I get some errors.

You could have a look at the WinHttp.au3 from @trancexx . However, the commands would then have to be adapted accordingly.

https://github.com/dragana-r/autoit-winhttp/blob/master/WinHttp.au3

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Developers

Include this info in the error message you get and show the result on error:

Global $__g_oHTTP_ErrorHandler = ObjEvent("AutoIt.Error", __HTTP_OnError) ; Install a custom error handler

; Custom error handler will set @error variable to 1 if a COM error is intercepted
; and will print out the error informations by the Console
Func __HTTP_OnError(ByRef $oMyError)
    Local $HexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("We intercepted a COM Error !" & @LF & _
            "Number is: " & $HexNumber & @LF & _
            "Windescription is: " & $oMyError.windescription _
            "err.description is: " & $oMyError.description _
            )
    Return SetError(5, $HexNumber, 0)
EndFunc   ;==>__HTTP_OnError

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

If I just add it like this:

#include <HTTP.au3>
Global $__g_oHTTP_ErrorHandler = ObjEvent("AutoIt.Error", __HTTP_OnError) ; Install a custom error handler

$url = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"
$postdata = "client_id=" & "API-ID" & "&" & "client_secret=" & "API-SECRET" & "&" & "grant_type=client_credentials"
$test = _HTTP_Post1($url, $postdata)
ConsoleWrite($test)   ;this consolewrite will show you your new token if its older than an hour

$token ="3997b2f1-1781-426d-bc1f-bd7e0bd9011f"    ;will need to be updated with latest token
$ans = _http_get1("https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags=845vls3")
MsgBox(0,"",$ans)

; Custom error handler will set @error variable to 1 if a COM error is intercepted
; and will print out the error informations by the Console
Func __HTTP_OnError(ByRef $oMyError)
    Local $HexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("We intercepted a COM Error !" & @LF & _
            "Number is: " & $HexNumber & @LF & _
            "Windescription is: " & $oMyError.windescription _
            "err.description is: " & $oMyError.description _
            )
    Return SetError(5, $HexNumber, 0)
EndFunc   ;==>__HTTP_OnError

Func _HTTP_Post1($url, $postdata = '')
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", $url, False)
    If @error Then Return SetError(1, 0, 0)

    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    $oHTTP.Send($postdata)
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Post

Func _HTTP_Get1($url)
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    Local $res = $oHTTP.Open("GET", $url, False)
    If @error Then Return SetError(1, 0, 0)
    #forceref $res
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.SetRequestHeader("Authorization", "Bearer " &$token)

    $oHTTP.Send()
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Get

I'll get the below error:
 

"C:\Users\DKent\Autoit\DellAPI\DellAPI.au3"(15,37) : error: __HTTP_OnError() already defined.
Func __HTTP_OnError(ByRef $oMyError)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\DKent\Autoit\DellAPI\DellAPI.au3"(20,35) : error: syntax error
            "err.description is: "
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\DKent\Autoit\DellAPI\DellAPI.au3 - 2 error(s), 0 warning(s)

 

Yours sincerely

Kenneth.

Link to comment
Share on other sites

  • Developers

Yes of course you get that.
So we are 13 years further and you don't know any basics at all and simply copy paste code in the hope this work and then dump it here so we can solve these simple things for you? 

Get rid of the #include when you hardcode the funcs in your own script and the second error is a simple basic mistake you really should be able to figure out.
This code is error free according au3check....  

Global $__g_oHTTP_ErrorHandler = ObjEvent("AutoIt.Error", __HTTP_OnError) ; Install a custom error handler

$url = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"
$postdata = "client_id=" & "API-ID" & "&" & "client_secret=" & "API-SECRET" & "&" & "grant_type=client_credentials"
$test = _HTTP_Post1($url, $postdata)
ConsoleWrite($test)   ;this consolewrite will show you your new token if its older than an hour

$token ="3997b2f1-1781-426d-bc1f-bd7e0bd9011f"    ;will need to be updated with latest token
$ans = _http_get1("https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags=845vls3")
MsgBox(0,"",$ans)

; Custom error handler will set @error variable to 1 if a COM error is intercepted
; and will print out the error informations by the Console
Func __HTTP_OnError(ByRef $oMyError)
    Local $HexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("We intercepted a COM Error !" & @LF & _
            "Number is: " & $HexNumber & @LF & _
            "Windescription is: " & $oMyError.windescription & _
            "err.description is: " & $oMyError.description _
            )
    Return SetError(5, $HexNumber, 0)
EndFunc   ;==>__HTTP_OnError

Func _HTTP_Post1($url, $postdata = '')
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", $url, False)
    If @error Then Return SetError(1, 0, 0)

    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    $oHTTP.Send($postdata)
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Post

Func _HTTP_Get1($url)
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    Local $res = $oHTTP.Open("GET", $url, False)
    If @error Then Return SetError(1, 0, 0)
    #forceref $res
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.SetRequestHeader("Authorization", "Bearer " &$token)

    $oHTTP.Send()
    If @error Then Return SetError(2, 0, 0)

    Local $sReceived = $oHTTP.ResponseText
    Local $iStatus = $oHTTP.Status
    If $iStatus = 200 Then Return $sReceived

    Return SetError(3, $iStatus, $sReceived)
EndFunc   ;==>_HTTP_Get

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thank you again I can now see more detalis than before.

 

"There were no credentials available for the client certificate" and that's funny, but maybe my credetials is wrong, but they shouldn't be.

Edited by Valnurat

Yours sincerely

Kenneth.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...