Search code examples
phpcurlphp-curldrchrono-api

How to get API Curl authorisation to work via PHP


I am working with dr chrono API and am trying to initialized the First step which is authorisation

The API documentation is here.

Here is an Authorisation sample:

https://drchrono.com/o/authorize/?redirect_uri=REDIRECT_URI_ENCODED&response_type=code&client_id=CLIENT_ID_ENCODED&scope=SCOPES_ENCODED

Here is what I have tried:

1.) I have tried the first code below

<?php

echo "<a href='https://drchrono.com/o/authorize/?redirect_uri=https://example_site.com/return_page.php&response_type=code&client_id=myclient-id-goeshere&scope=BASE_SCOPE:[read|write]'>Authorize</a>";


?>

but when the page redirects it displays error of Invalid_scope. Below is the error link returned.

https://example_site.com/return_page.php?error=invalid_scope

2.) Using Curl

$ci = 'my-client-id-goes-here';
$ci_encode = urlencode($ci);

$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'response_type'=>'code', 
    'client_id'=>$ci_encode,
    'redirect_uri'=>$uri_encode,
    'scope'=>'BASE_SCOPE:[read|write]',
]));
$response = curl_exec($ch);

curl_close($ch);

print_r($response);

Using curl code above does even redirect me at all.

I think the major problem is the scope not properly set. How can I solve this issue?

Update

Code section for curl:

$ci = 'my-client-id';
$ci_encode = urlencode($ci);
$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/?";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'response_type'=>'code', 
    'client_id'=>$ci,
    'redirect_uri'=>$uri,
    'scope'=>'patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write'
]));
$response = curl_exec($ch);

curl_close($ch);

print_r($response);

Solution

  • Instead of BASE_SCOPE:[read|write] I would use patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write

    The docs say that you can choose among user, calendar, patients, patients:summary, billing, clinical and labs and compose a scope out of these values.