You can generate the access token for a user in Bold Reports by using the Authentication API. To do this, pass the following parameters:
username
- The user’s email address.
password
- The user’s password.
grant_type
- The type of credentials used to authorize the request for an access token. The valid value is password.
Use HTTPClient
with ASP.NET Core
, ASP.NET MVC
, and ASP.NET Web Forms
applications to interact with RESTful APIs. Refer to the following code to generate the authorization using the credentials:
public class TokenHelper
{
public string GenerateToken()
{
System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
string userName = "user@example.com"; //Provide your Email ID
string password = "demo"; //Provide your Password
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("password", password)
});
var result = client.PostAsync("https://demo.example.com/reporting/api/site/site1/token", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
var token = JsonConvert.DeserializeObject<Token>(resultContent);
return token.token_type + " " + token.access_token;
}
}
}
public class Token
{
public string access_token { get; set; }
public string token_type { get; set; }
public string expires_in { get; set; }
public string userName { get; set; }
public string serverUrl { get; set; }
public string password { get; set; }
}
Use Ajax
to interact with RESTful APIs. Refer to the following code to generate the authorization using the credentials:
<script type="text/javascript">
$(function () {
var dataValue = "";
var apiRequest = new Object();
apiRequest.password = "demo"; //Provide your Password
apiRequest.userid = "user@example.com"; // Provide your Email ID
$.ajax({
type: "POST",
url: "https://demo.example.com/reporting/api/site/site1/get-user-key",
data: apiRequest,
success: function (data) {
dataValue = data.Token;
var token = JSON.parse(dataValue);
}
});
});
</script>
Use the Http
module to interact with RESTful APIs. Refer to the following code to generate the authorization using the credentials:
function token(res, callback, callbackInput) {
var postData = JSON.stringify({
userid: 'user@example.com', // Provide your Email ID
password: 'demo' // Provide your Password
})
var options = {
method: 'POST',
host: 'demo.example.com',
port: 443,
path: '/reporting/api/site/site1/get-user-key',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
var request = gethttp().request(options, function (response) {
var data = '';
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function () {
var tokenData = JSON.parse(data);
var tokenObject = JSON.parse(tokenData.Token);
callbackFunction(res, tokenObject.token_type + ' ' + tokenObject.access_token, callback, callbackInput);
});
});
request.on('error', error => {
console.error(error)
})
request.end();
}
See Also