Want to get bearer token to connect to API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 05:54 AM - edited 10-01-2024 05:57 AM
Hi i want to get bearer token by providing client id and client secret to then connect the third party API.
In postman i can provide end point, client_id and client_secret(in headers) and get the following
"requested resource not found"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 06:48 AM
hi @cloudv
you need to make a POST request instead of a GET request, as most OAuth 2.0 token endpoints require the token request to be sent as a POST. Additionally, the client_id & client_secret are typically included in the body of the request rather than as headers.
Following is sample script to obtain 'Bearer token' (you can utilize it in background script / script include)
(function() {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint("https://openapi.service.TEST.net/v1/token");
request.setHttpMethod("POST"); // Use POST instead of GET
request.setRequestHeader("Content-Type", "application/json");
// Set the body of the request with client_id and client_secret
var requestBody = {
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET"
};
request.setRequestBody(JSON.stringify(requestBody));
// Execute the request
var response = request.execute();
// Check the response
var statusCode = response.getStatusCode();
var responseBody = response.getBody();
gs.print("Status Code: " + statusCode);
gs.print("Response Body: " + responseBody);
// If needed, you can parse the response
if (statusCode == 200) {
var jsonResponse = JSON.parse(responseBody);
gs.print("Access Token: " + jsonResponse.access_token);
} else {
gs.print("Error: " + responseBody);
}
})();
i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 07:47 AM - edited 10-01-2024 07:47 AM
Hi @Rajesh Chopade1 , I get the following error code
{"error":"bad request","description":"invalid value, field: grant_type, constraint: required"}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 09:26 AM
hi @cloudv
As per the error we need to include the grant_type in your request body.
Replace
// Set the body of the request with client_id and client_secret
var requestBody = {
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET"
};
with
// Set the body of the request with client_id, client_secret, and grant_type
var requestBody = {
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"grant_type": "client_credentials" // Add the grant_type
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 06:51 AM
You shouldn't need to do this manually, instead you can create an application registry, add in the details such as client ID, secret, and URL. You can then use that application registry in future API calls.