Jump to content

Winhttp API help


Recommended Posts

This is my first try with this.  I feel like I am very close, but I just cant make it work.

#include<WinHttp.au3>

Opt("MustDeclareVars", 1)

; !!!Note that this example will fail because of invalid username and password!!!

Global $sAddress = "app.energycurb.com"
Global $sUserName = ""
Global $sPassword = ""
; Post data:
Global $sPostData =  'grant_type=password&username='&$sUserName&'&password='&$spassword
MsgBox(0,"",$spostdata)

; Initialize and get session handle
Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5")

; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sAddress, $INTERNET_DEFAULT_HTTPS_PORT)

; Make a request
Global $hRequest = _WinHttpOpenRequest($hConnect, _
        "POST", _
        "oauth2/token", _
        Default, _
        Default, _
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", _
        $WINHTTP_FLAG_SECURE)

; Add header fields to the request
_WinHttpAddRequestHeaders($hRequest, "Accept-Language: en-us,en;q=0.5")
_WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded")
_WinHttpAddRequestHeaders($hRequest, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
_WinHttpAddRequestHeaders($hRequest, "Keep-Alive: 300")
_WinHttpAddRequestHeaders($hRequest, "Connection: keep-alive")




; Send it
_WinHttpSendRequest($hRequest, -1 , $sPostData)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
; Check if there is a response
Global $sHeader, $sReturned
If _WinHttpQueryDataAvailable($hRequest) Then
    $sHeader = _WinHttpQueryHeaders($hRequest)
    MsgBox(64, "Header", $sHeader)
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
    ; Print returned
    ConsoleWrite($sReturned)
Else
    ConsoleWriteError("!No data available." & @CRLF)
    MsgBox(48, "Failure", "No data available.")
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I am trying to request an API token described here...

http://docs.energycurb.com/authentication.html#authenticating-a-request

It appears I would simple send a post that looks like this....  grant_type=password&username='&$sUserName&'&password='&$spassword but I get "bad password".  I must have a parameter wrong somewhere.

most of this code is taken from examples using my own values

 

Edited by wisem2540
Removed username & password
Link to comment
Share on other sites

  • Moderators

Even if this had been bumped too early (it wasn't), surely not necessary to report the thread rather than just informing the poster of the forum etiquette...

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

It appears your URL may be incorrect.  The documentation says: 

Quote

You are using the base URL for the request.

On 10/29/2016 at 10:15 PM, wisem2540 said:

Global $sAddress = "app.energycurb.com"

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Sorry it took so long to get back to you

@Jfish Yes, I have tried it both ways.  But, I was following an example I found where the rest in the the request.

Global $hRequest = _WinHttpOpenRequest($hConnect, _
        "POST", _
        "oauth2/token", _
        Default, _
        Default, _
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", _
        $WINHTTP_FLAG_SECURE)

 

 

Edited by wisem2540
Link to comment
Share on other sites

Without credentials to test the only other thing I would suggest is make sure you are passing what they expect for username and password "the client token and client secret being the username and password, respectively."  Otherwise, I can't test anything without an account.

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Creds don't work for me either - agree you should not post them.

Quote

{
    "status": "401 Unauthorized",
    "code": 401,
    "explanation": "This server could not verify that you are authorized to access the document you requested.  Either you supplied the wrong credentials (e.g., bad password), or your browser does not understand how to supply the credentials required.",
    "detail": "Invalid client credentials"
}

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Hopefully this should work:

#include <WinHttp.au3>

Opt("MustDeclareVars", 1)
Opt("TrayIconDebug", 1)

Global $hOpen = _WinHttpOpen('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5') ; Initialize and get session handle
Global $sAddress = 'app.energycurb.com'
Global $hConnect = _WinHttpConnect($hOpen, $sAddress, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle
Global $hRequest = _WinHttpOpenRequest($hConnect, 'POST', '/oauth2/token', Default, 'https://app.energycurb.com/', Default, $WINHTTP_FLAG_SECURE) ; Make a request

; Add header fields to the request
_WinHttpAddRequestHeaders($hRequest, 'Accept: */*')
_WinHttpAddRequestHeaders($hRequest, 'Accept-Language: en-US,en;q=0.5')
_WinHttpAddRequestHeaders($hRequest, 'Accept-Encoding: gzip, deflate, br')
_WinHttpAddRequestHeaders($hRequest, 'Authorization: Basic czFkZmw3amJ4b3Y1cHRoMXJ4enNjMGZsNDgweno2cndnNHVvNmJ1MGd6dTF0ODkzOTNjc2p3cHM2ZzVsc2djeDo4ZHBkem0yYTZtY3lnM3hvY2ZxdmZyYWpmaW42eWFqamRvcW1oajc3eW52a3NteWFxdzZycHZwcG41c3JkZTZk')
_WinHttpAddRequestHeaders($hRequest, 'Content-Type: multipart/form-data; boundary=---------------------------78432868715746')
_WinHttpAddRequestHeaders($hRequest, 'origin: https://app.energycurb.com')
_WinHttpAddRequestHeaders($hRequest, 'Content-Length: 384')
_WinHttpAddRequestHeaders($hRequest, 'Connection: keep-alive')

Global $sUserName = 'me@email.com' ;Replace user name, obviously
Global $sPassword = 'password' ;Replace password, obviously
Global $sPostData = '-----------------------------78432868715746' & @CRLF & _
        'Content-Disposition: form-data; name="grant_type"' & @CRLF & @CRLF & _
        'password' & @CRLF & _
        '-----------------------------78432868715746' & @CRLF & _
        'Content-Disposition: form-data; name="username"' & @CRLF & @CRLF & _
        $sUserName & @CRLF & _
        '-----------------------------78432868715746' & @CRLF & _
        'Content-Disposition: form-data; name="password"' & @CRLF & @CRLF & _
        $sPassword & @CRLF & _
        '-----------------------------78432868715746--' & @CRLF
_WinHttpSendRequest($hRequest, Default, $sPostData) ; Send it
_WinHttpReceiveResponse($hRequest) ; Wait for the response

; Check if there is a response
Global $sHeader, $sReturned
If _WinHttpQueryDataAvailable($hRequest) Then
    $sHeader = _WinHttpQueryHeaders($hRequest)
    MsgBox(64, 'Header', $sHeader)
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
    ConsoleWrite($sReturned) ; Print returned
Else
    ConsoleWriteError('!No data available.' & @CRLF)
    MsgBox(48, 'Failure', 'No data available.')
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

 

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...