API Beginning To Use


#1

I’m really scratching my head how to use your API. I’ve created a Client Application and have those details. What is the point of having a Redirect URI and Website listed here? All I want to do is make a request to the API and for the API to return the status of all my units in a specific group.

I can easily click the “Get Access Token” on the documentation page and mess around with the API. The API call request I want to use looks like this.
https://api.ic.peplink.com/rest/o/MyOrg/g/MyGroup/d?access_token=MyToken

I’m finding it hard to figure out how to program this. I have worked with other API interfaces and this one seems a bit different with the Redirect URI and what not. I don’t want to be redirected anywhere I just want to pass information to get a key and get the proper response to get a token. I need some help.


#2

Using OAuth on InControl2 is quite straight forward, you may refer to below shell script

http://download.peplink.com/files/ic2/sample_ic2_api_client.sh

OAuth has different grant type, depends on the your application needs, to obtain the access token with client ID and client secret, you may grant access thru the “client_credentials” flow, for detail information regarding OAuth, please refer to below URL.

With RESTful client

  1. Login to InControl2
  2. In any organization/network/device overview, on the top right hand corner, click on your login email address before the “Sign out” link, that would bring you to account setting page.
  3. At the bottom of the page, there is a “Client Application” section, click on “New Client”
  4. Enter the name and checked “Enable”, other fields can be leave blank, then click “Save”
  5. Click on the application name that you just created, you should see “Client ID” and “Client Secret” at the bottom of the popup window

Now you can obtain a token with that client ID and secret

With HTTP Call

POST https://api.ic.peplink.com/api/oauth2/token
Encoding: application/x-www-form-urlencoded
Data: client_id=[your_client_id]&client_secret=[your_client_secret]&grant_type=client_credentials

Or with CURL command:

curl -X POST -H “Content-Type: application/x-www-form-urlencoded” --data “client_id=[your_client_id]&client_secret=[your_client_secret]&grant_type=client_credentials” https://api.ic.peplink.com/api/oauth2/token

If success, you will receive response like below

{“access_token”:“your_access_token”,
“expires_in”: 604799,
“token_type”:“Bearer”,
“refresh_token”:“your_refresh_token”
}

Now you can access our system with access token, please refer to http://www.peplink.com/ic2-api-doc/ for supported calls.

e.g.
https://api.ic.peplink.com/rest/o?access_token=[your_access_token]

Hope above helps.

Billy
The Peplink Team


#3

Thank you for the information this was very much more helpful than the online resources.

For those using C# as well. Here is my code to get a token. Thanks for the help!

var request = (HttpWebRequest)WebRequest.Create(“https://api.ic.peplink.com/api/oauth2/token”);
var postData = “client_id=MyClientId”;
postData += “&client_secret=MySecret”;
postData += “&grant_type=client_credentials”;
var data = Encoding.ASCII.GetBytes(postData);
request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();


#4

hello, I’m trying to use the api using the curl function in php, I already have my client code and my secret code but I do not know what value I should put in the grant_type = client_credentials. what value is the client_credentials.
Thank you


#5

Hi,

The parameter name is grant_type, the value, in this case, is always “client_credentials”, so just put “grant_type=client_credentials” as is will do.

Billy