API documentation
Processing your reviews with Octopinion is very easy. Follow the two steps below:
Get the access token
The access token is a security check while using Octopinion API to make sure no one else can send texts to be processed. The access token must be sent in the processing texts requests. Access tokens can only be generated with the product key.
Access Token generation strategies
The are two strategies that you can use while generating access tokens:
- Generate an access token to be used only once. This can be accomplished by sending 'ONE', in the get token request, as a value of the query parameter tokenType . The access token retrieved will work only once and it will expire after 5 minutes. This is the most secure way if you want to call the API from the client side.
- Generate an access token that is usable until another one is created. This can be accomplished by sending 'TILL' in the get token request, as a value of the query parameter tokenType. This strategy is simpler than the first one, however, it is not as safe as the first one. We do not recommend this strategy if using client side to send data.
Token generation
In order to generate the access token you must call:
https://www.octopinion.co.uk/token/{username}
The product key must be sent as a request parameter. Your product key should be properly well saved within your environment and never be shared outside.
Code examples
Java
A basic code example using Spring RestTemplate for retrieving the access token:
RestTemplate
restTemplate
=
new
RestTemplate();
String
getTokenUrl
=
"https://www.octopinion.co.uk/token/{username}"
;
UriComponentsBuilder
builder
=
UriComponentsBuilder.fromHttpUrl(
getTokenUrl
)
.queryParam(
"key"
,
"{Your Product key}"
)
.queryParam(
"tokenType"
"{TILL or ONE}"
);
// tokenType values can be TILL or ONE. See the tokenType strategy explanation abobe.
HttpHeaders
headers
=
new
HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.
APPLICATION_JSON
));
HttpEntity<?>
entity
=
new
HttpEntity<>(
headers
);
ResponseEntity<String>
result
=
restTemplate
.exchange(
builder
.build().encode().toUri()
, HttpMethod.
GET
,
entity
,
String.
class
);
ObjectMapper
om
=
new
ObjectMapper();
//Get an object from the json response
TokenResponse
tr
=
om
.readValue(
result
.getBody(), TokenResponse.
class
);
PHP
A basic code example using PHP for retrieving the access token:
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
"https://www.octopinion.co.uk/token/{username}?key={Your Product Key}&tokenType={TILL or ONE}"
);
curl_setopt(
$ch
, CURLOPT_GET,
true
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER,
true
);
$result
= curl_exec(
$ch
);
$json
= json_decode(
$result
,
true
);
//get the data within the array.
echo
$json
[
"status"
]
;
Node/Typescript
A basic code example using NPM request package for retrieving the access token:
import
*
as
request
from
'request'
;
· · ·
class
ExampleClass
{
· · ·
private
getAccessToken
()
{
var
data
= {
'tokenType'
:
'TILL'
,
'key'
:
'{Your Product Key}'
};
request
.
get
(
'https://www.octopinion.co.uk/token/{username}'
,
{
qs
:
data
,
(
err
,
httpResponse
,
body
) => {
JSON
.
parse
(
body
).
token
;
}
· · ·
}
Token response
The response from the Octopinion API will be an object like the following:
{token: "the generated token", status: "OK / INTERNAL_SERVER_ERROR", message: "message"}
The status field will be OK if the token has been successfully generated or INTERNAL_SERVER_ERROR if a problem has occurred.
The message field will contain the error or a successful message.
Processing texts
The API url
In order to process texts you must call the following url:
https://www.octopinion.co.uk/evaluate/{section}/{user}
The access token and the text to analyse must be sent as request parameters.
Code examples
A code example using jquery:
$.ajax({
url:
"https://www.octopinion.co.uk/evaluate/section/{username}"
,
data : {
text:
"this is a good text to analyze"
,
token:
"{Your Access Token}"
,
},
type:
"get"
,
dataType:
"json"
,
success:
function
(
data
) {
if
(
data
.status ===
'OK'
) {
// do something with the data
}
if
(
data
.status ===
'INTERNAL_SERVER_ERROR'
) {
// do something with the registered error.
}
},
error:
function
(
e
) {
// something went wrong. Check the error message. This happens when an unexpected error happens.
}
});
A basic code example using Spring RestTemplate:
RestTemplate
restTemplate
=
new
RestTemplate();
String
evaluateUrl
=
"https://www.octopinion.co.uk/evaluate/section/{username}"
;
UriComponentsBuilder
builder
=
UriComponentsBuilder.fromHttpUrl(
evaluateUrl
)
.queryParam(
"text"
,
"Text to analyze"
)
.queryParam(
"token"
,
"{Your Access Token}"
)
;
HttpHeaders
headers
=
new
HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.
APPLICATION_JSON
));
HttpEntity<?>
entity
=
new
HttpEntity<>(
headers
);
ResponseEntity<String>
result
=
restTemplate.exchange(
builder
.build().encode().toUri()
, HttpMethod.
GET
,
entity
,
String.
class
);
ObjectMapper
om
=
new
ObjectMapper();
//Get an object from the json response
AnalizedResponse
analizedResponse
=
om
.readValue(
result
.getBody(), AnalizedResponse.
class
);
A basic code example using PHP:
$ch
= curl_init();
$data
=
array(
"token"
=>
"{Your Access Token}"
,
"text"
=>
"Text to analyze"
);
curl_setopt(
$ch
, CURLOPT_URL,
"https://www.octopinion.co.uk/evaluate/section/{username}?"
.http_build_query(
$data
));
curl_setopt(
$ch
, CURLOPT_GET,
true
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER,
true
);
$result
= curl_exec(
$ch
);
$json
= json_decode(
$result
,
true
);
//get the data within the array.
echo
$json
[
"negativeFound"
];
A basic code example using Node and typescript:
import
*
as
request
from
'request'
;
· · ·
class
ExampleClass
{
· · ·
private
sendReview
(
text
:
string
,
section
:
string
)
{
var
data
= {
'text'
:
text
,
'token'
:
'{Your Access Token}'
};
request
.
get
(
'https://www.octopinion.co.uk/evaluate/{section}/{username}'
,
{
qs
:
data
,
(
err
,
httpResponse
,
body
) => {
JSON
.
parse
(
body
)
;
}
· · ·
}
API response
The response from the Octopinion API will be an object like the following:
{negativeFound: [{word:""}], positiveFound: [{word:""}], processedText: "The processed text", status: "OK / INTERNAL_SERVER_ERROR", message:
"message"}
The processed text is basically the same text sent to the server. Identified negatives and positives will be highlighted.
The status field will be OK if the text has been processed successfully or INTERNAL_SERVER_ERROR if a problem has occurred.
The message field will be either an error message or a successful message.
The negativeFound and positiveFound will be an array of elements for each positive or negative found with its respective identified word if found any.
The word is the identified subject that the positive or negative refers to.