Authentication with the API mandates the inclusion of a bearer authentication token within the header.
Upon provision by Cober, you will be furnished with a designated username (email) and corresponding password. This credential set is to be utilized for authentication, facilitating the acquisition of your bearer token.
It is imperative to integrate this bearer token into all subsequent interactions with the API. It's worth noting that this token is perpetually valid and does not undergo expiration.
$ch = curl_init('https://coberapi.com/api/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=sample@sample.com&password=password');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$jsonData = json_decode($response, true);
$yourToken = $jsonData['token'];
const https = require('https');
const data = 'email=sample@sample.com&password=password';
const options = {
hostname: 'coberapi.com',
path: '/api/login',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
const jsonData = JSON.parse(responseData);
const yourToken = jsonData.token;
console.log(yourToken);
});
});
req.write(data);
req.end();
using System;
using System.IO;
using System.Net;
using System.Text;
class Program
{
static void Main()
{
string url = "https://coberapi.com/api/login";
string data = "email=sample@sample.com&password=password";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{
string responseData = reader.ReadToEnd();
dynamic jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseData);
string yourToken = jsonData.token;
Console.WriteLine(yourToken);
}
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
String url = "https://coberapi.com/api/login";
String data = "email=sample@sample.com&password=password";
URL urlObject = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.writeBytes(data);
outputStream.flush();
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
String responseData = response.toString();
org.json.JSONObject jsonData = new org.json.JSONObject(responseData);
String yourToken = jsonData.getString("token");
System.out.println(yourToken);
}
} else {
System.out.println("HTTP request failed with response code: " + responseCode);
}
connection.disconnect();
}
}