Strange behaviour while Dashboard API

Skackar
Here to help

Strange behaviour while Dashboard API

I am trying to the access the Network & devices list registered within the organization using DASHBOARD API, but its not behaving correctly. 

As i am able to see that request redirecting to 404 when hitting via browser (Chrome), when i haven't loggedin to the Dashboard panel.

But as i loggedin to the Dashboard panel, i am able to see the Network details at the Browser (Chrome), but not displayed to any other browsers(FireFox, Safari)

When hitting it via POSTMAN it's working fine, whether i loggedin or not to the Meraki Dashboard. 

 

So, why its behaving awkwardly, can you please tell me. If that is the case then, Dashboard API is of no use for any one if i am not able to use or have to login each time to retrieve the dashboard details.

 

Using PHP cURL for fetching the details.

 

Thanks

6 Replies 6
BrechtSchamp
Kind of a big deal

Not sure if I understand your problem correctly.

 

The dashboard API doesn't depend on whether you're logged in to the dashboard or not. It uses a separate API key linked to your account. So even if you are using a php page that uses the API in the backend being logged in or not shouldn't have an influence. That API key is added to the HTTP GET headers of your API calls by specifying the header X-Cisco-Meraki-API-Key.

 

That being said however, if you do try to access an API get directly in your browser, you can actually fetch the same data in the browser using your normal login session... without the API key. Hence it working in Chrome (where you're logged in) but not in firefox and safari.

 

In short, you should add the X-Cisco-Meraki-API-Key somehow to your cURL. Something like:

curl_setopt($curlobject, CURLOPT_HTTPHEADER, array('X-Cisco-Meraki-API-Key: <your API key>'));

 

Keep in mind that postman can also generate a code snippet for you and "PHP curl" is one of the supported formats:

https://learning.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets/

Skackar
Here to help

I have already the Key but not worked for me. I tried it with PHP shell script and it working fine. I don't know what's problem with the Dashboard API. Not working with curl.

BrechtSchamp
Kind of a big deal

Well I have no PHP server atm to test but this is the code snippet generated by Postman. I used the organizations get call to start with something simple. Once you get that working, go further.

 

Perhaps compare your code to this to see if there's anything you're missing:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.meraki.com/api/v0/organizations",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "Postman-Token: abcdef01-234567890-123456-78901234", // random token generated by postman as workaround to some bug in chrome
    "X-Cisco-Meraki-API-Key: abcdef012345678901234567890123456789012", // replace with your API key
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Skackar
Here to help

Didn't worked sir..... I tried your code. I was trying to fetch the Network & devices list registered within an organization.

Nash
Kind of a big deal

Obvious question is obvious, but are you certain that the dashboard API is enabled on the organization(s) that you are attempting to work with?

BrechtSchamp
Kind of a big deal


@Nash wrote:

Obvious question is obvious, but are you certain that the dashboard API is enabled on the organization(s) that you are attempting to work with?


Indeed, that's why I recommended to start with the organizations get and work from there. It will work regardless of specific network settings. As soon as you have an API key, it works.

Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.
Labels