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
rrhandle8Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
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.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
just saw your attachment; where is the X-Originating-IP argument in your strPostString?

Jim.
rrhandle8Author Commented:
"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?
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<<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.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
I should add that the API notes would tell you if URL Encoding needed to be done or not.

Jim.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrhandle8Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.