Link to home
Start Free TrialLog in
Avatar of rrhandle8
rrhandle8Flag for United States of America

asked on

Constant Contact API REST with VBA

Need help getting this right.

Keep getting this error: [{"error_key":"http.status.unauthorized","error_message":"No authentication is present."}]

Sub Bentely()

strPostURL = "https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=[xxxxxxxxxxxxxxxxx]"
strPostString = "Authorization: Bearer [xxxxxxxxxxxxxxxxxxx]"
Debug.Print strPostString

Dim objRequest As New MSXML2.XMLHTTP
objRequest.Open "POST", strPostURL, False
objRequest.Send strPostSting
strPostResponse = objRequest.responseText
Debug.Print strPostResponse
Set objRequest = Nothing

End Sub

Open in new window

API.png
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

You need to look at the documentation for the web site your connecting to.

Check your Post URL string carefully and also the post string.

Jim.
just saw your attachment; where is the X-Originating-IP argument in your strPostString?

Jim.
Avatar of rrhandle8

ASKER

"where is the X-Originating-IP argument in your strPostString?"

Messing around with it, I did remove that, but when it was there I still got the error.

Jim, the api.png is supposed to give me everything I need, isn't it?
<<Jim, the api.png is supposed to give me everything I need, isn't it? >>

 It should, although I'm assuming that's an example from the web site and not API documentation specifically.

 You really should look at the API reference and check each of the possible arguments and if they are needed or not.  Examples are typically meant to be simple and may not include everything you need.

  As far as what your doing in the code, I don't see anything wrong except for possibly one thing; some web sites require URL encoding for spaces in arguments.  The procedure for that is this:

Public Function URLEncode(xxx As String) As String
10        URLEncode = Replace(xxx, " ", "%20")
End Function

Open in new window


Then when your building up your strings, it would look like this:

110               strPostURL = "https://secure.authorize.net/gateway/transact.dll"
120               strPostSting = ""
130               strPostSting = strPostSting & "x_login=" & URLEncode(strAPILogin) & "&"
140               strPostSting = strPostSting & "x_tran_key=" & URLEncode(strTransactionKey) & "&"
                  'For debugging.
                  'strPostSting = strPostSting & "x_test_request=" & URLEncode("TRUE") & "&"
150               strPostSting = strPostSting & "x_version=" & URLEncode("3.1") & "&"
160               strPostSting = strPostSting & "x_delim_data=" & URLEncode("TRUE") & "&"
170               strPostSting = strPostSting & "x_delim_char=" & URLEncode("|") & "&"
180               strPostSting = strPostSting & "x_relay_response=" & URLEncode("FALSE") & "&"
190               strPostSting = strPostSting & "x_email_customer=" & URLEncode("FALSE") & "&"

200               strPostSting = strPostSting & "x_type=" & URLEncode("PRIOR_AUTH_CAPTURE") & "&"
210               strPostSting = strPostSting & "x_trans_id=" & URLEncode(rs!CCTransactionID) & "&"

                  ' Additional fields can be added here as outlined in the AIM integration
                  ' guide at: http://developer.authorize.net
220               strPostSting = left(strPostSting, Len(strPostSting) - 1)

                  ' We use xmlHTTP to submit the input values and record the response
                  Dim objRequest As New MSXML2.XMLHTTP
230               objRequest.Open "POST", strPostURL, False
240               objRequest.Send strPostSting
250               strPostResponse = objRequest.responseText
                  'Debug.Print strPostResponse
260               Set objRequest = Nothing

Open in new window



 As you can see, I do exactly what your doing, so the error your getting is about the data your passing, not what your doing as an overall process in VBA.

Jim.
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jim gave me some good information.  Here is the working solution for anyone who faces this problem in the future.

strPostURL = "https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx"
strpoststring = "xxxxxxxxxxxxxxxxxxxxxxxx"

Debug.Print strpoststring

Dim objRequest As New MSXML2.XMLHTTP
objRequest.Open "Get", strPostURL, False
objRequest.setRequestHeader "Authorization", "Bearer " & "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


objRequest.send
strPostResponse = objRequest.responseText
Debug.Print strPostResponse
Set objRequest = Nothing