How do I get a token with a user name and password?

You can generate the authentication 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 are used to authorize the request for an authentication token. The valid value is: password.

How to generate the token in .NET, .NET Core, and .NET Framework

In .NET applications, post the user’s credentials with grant_type set to password to the /reporting/api/token endpoint with HttpClient to get a bearer token.

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 authentication token using the credentials:

 public  class TokenHelper
    {
        public string GenerateToken(string userName, string password)
        {
            System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
            string userName = "[email protected]"; //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/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; }
    }

How to generate the token in JavaScript

In JavaScript, post the user’s email address and password to the /reporting/api/get-user-key endpoint with jQuery Ajax to get the token.

Use Ajax to interact with RESTful APIs. Refer to the following code to generate the authentication token using the credentials:

   <script type="text/javascript">
         $(function () {
               var dataValue = "";
               var apiRequest = new Object();
               apiRequest.password = "demo"; // Provide your Password
               apiRequest.userid = "[email protected]"; // Provide your Email ID
               $.ajax({
                  type: "POST",
                  url: "https://demo.example.com/reporting/api/get-user-key",
                  data: apiRequest,
                  success: function (data) {
                     dataValue = data.Token;
                     var token = JSON.parse(dataValue);
                  }
               });
         });
   </script>

How to generate the token in Node.js

In Node.js, post the user’s email address and password to the /reporting/api/get-user-key endpoint with the http module to get the token.

Use Http module to interact with RESTful APIs. Refer to the following code to generate the authentication token using the credentials:

function token(res, callback, callbackInput) {
  var postData = JSON.stringify({
    userid: '[email protected]', // Provide your Email ID
    password: 'demo' // Provide your Password
  })
  var options = {
    method: 'POST',
    host: 'demo.example.com',
    port: 443,
    path: '/reporting/api/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();
}

Frequently asked questions

How do I use the token in the Report Viewer or Web Report Designer?

Pass the token with the bearer prefix in the serviceAuthorizationToken property. The Bold Reports® Cloud Report Viewer and Web Report Designer use it to authenticate with the Report Server.

What does the C# sample return?

The C# sample returns token_type and access_token joined by a space. You can use that string as the Authorization header value.

Can I generate a token without the user’s password?

Yes. Use the Embed Secret Key API. The request is signed with the embed secret key instead of a password.

See Also