PHP: Calling DocuSign userinfo endpoint after OAuth returns an "unauthorized_client" error

Viewed 52

I want to get the user info (and user id in particular) after having my user go through the OAuth flow.

I try to obtain an access code from the return "code" queryparameter, but I get an "unauthorized_client" error

Here is my relevant code:

$url = 'https://account.docusign.com/oauth/token'; 
$cURLConnection = curl_init($url);
curl_setopt($cURLConnection, CURLOPT_URL, $url);
curl_setopt($cURLConnection, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

//create basic authorization
$integration_key = '...2dee';
$secret_key = '...c11a';
$headers = array(
   "Authorization: Basic " . base64_encode($integration_key . ':' . $secret_key)
);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, $headers);

$body = array(
    "grant_type" => "authorization_code",
    "code" => $_REQUEST['code']
);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $body);

$access_token = json_decode(curl_exec($cURLConnection), true);
print_r($access_token);

The error is provided by the print_r of $access_token

Thanks!

1 Answers

I believe your problem is that you're sending the POST request's body encoded as JSON. That's not right, according to both the OAuth standards and the DocuSign documentation, the body must use form encoding:

  1. Set the Content-Type header to application/x-www-form-urlencoded
  2. See the example request body
...
--data "grant_type=authorization_code&code=eyJ0eXAi.....QFsje43QVZ_gw"
Related