2
votes

I am trying to implement the paypal REST API for an application in ColdFusion. I have my application set up in paypal so I have the client_id & secret key's.

https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/

This URL shows an example curl call that I am trying to reproduce in CF:

curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"

here is my CF call (modified the keys a little for my test account)

<cfhttp method="post" url="https://api.sandbox.paypal.com/v1/oauth2/token"  result="test">
<cfhttpparam type="header" name="authorization" value="ASfK_BCZ54849na-kMSKvrKEk4WNDkoIikQlTfsI3nS-ghY1VTzH5q2pU:EC-7qhACEQ7XGjo2dU4gFPJDH3Et0KeMx0Z5Xmbf9PnhPE5diq-CO" >
<cfhttpparam type="header" name="content-type" value="application/x-www-form-urlencoded" >
<cfhttpparam type="formfield" name="grant_type" value="client_credentials" >

The response I receive is "invalid_client" - "invalid client credentials". The docs state that the auhorization should be passed in the form "client_id:secret". I have played around changing the names of my field's, tried passing it in the header and tried passing it as a formfield, all to no avail. Best I can tell, paypal does not require a signature method and states it uses basic http auth.

Can anyone see what I am missing here?

2

2 Answers

5
votes

I'm not sure why your solution was not working for you, but not me. It got me started, but I needed to tweak mine to get the request to go through. Reverse engineering the PHP script I came up with this:

<cfset clientid = "***************************"/>
<cfset secret = "***************************"/>

<cfhttp method="post" url="https://api.sandbox.paypal.com/v1/oauth2/token" result="local.test">
    <cfhttpparam type="header" name="Content_Type" value="application/json" >
    <cfhttpparam type="formfield" name="grant_type" value="client_credentials" >
    <cfhttpparam type="header" name="Authorization" value="Basic #ToBase64(clientid & ":" & secret)#">
</cfhttp>

I hope this saves someone else 3 hours of their Saturday. Now, I'm off to figure out the next steps. If anyone is interested in wrapping this up into a full SDK like solution for the PayPal REST API let me know, I might be interested in collaborating.

2
votes

I am answering here from memory, but I seem to recall that it was required to set the content type to application/json, you set it to application/x-www-form-urlencoded.