1
votes

I normally don't ask questions here because most of the times I can find answers. But at this moment I haven“t find one, and I am really stuck. I have a problem trying to get access token for the Cherwell api: http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token I used postman to generate this code:

This is relevant to Cherwell Service Management's V8+ REST API.

Code that throws server run time exception:

 string user = "myUser";
    string password = "myPassword";

    var client1 = new RestClient("http://13.88.176.216/cherwellapi/token?auth_mode=Internal");
client.Authenticator = new HttpBasicAuthenticator(user, password);
    var request1 = new RestRequest(Method.POST);
    request1.AddHeader("content-type", "application/x-www-form-urlencoded");

    request1.AddHeader("cache-control", "no-cache");
    request1.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=my_client_id&client_secret=my_client_secret", ParameterType.RequestBody);
    IRestResponse response = client1.Execute(request1);

The thing is when I execute the same method from the swagger ui (http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token) I can get the token without getting any error.

Details of the request in CURL:

Curl

curl -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" -d "grant_type=password&client_id=my_client_id&client_secret=my_client_secret&username=my_user_name&password=my_password" "http://13.88.176.216/CherwellAPI/token?auth_mode=Internal"

Request URL http://13.88.176.216/CherwellAPI/token?auth_mode=Internal

This is the response body from the swagger ui test, not my code:

{
  "access_token": "the_acces_token",
  "token_type": "bearer",
  "expires_in": 1199,
  "refresh_token": "the_refresh_token",
  "as:client_id": "client_key",
  "username": "user",
  ".issued": "date",
  ".expires": "other_date"
}

Any help will be appreciated.

2

2 Answers

0
votes

Try including the username/password as part of your form encoded data.

Drop the authenticator section, it shouldn't be necessary for this part.

So,

request1.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=my_client_id&client_secret=my_client_secret&username=(yourusernamehere)&password=(yourpasswordhere)", ParameterType.RequestBody);

I actually just recorded a video on this not too long ago (using a browser rest client, not C#, but you get the picture), that should post to our youtube channel soon at https://youtube.com/beyond20llc - I can send this video to you if you'd like to see it before it reaches youtube.

The data I sent when I was authenticating for a token essentially looked like the following:

grant_type=password&

client_id=1234567890&

username=CSDAdmin&

password=CSDAdmin

(Of course, CSDAdmin being the default username/password on a fresh installation of Cherwell - if you're CSDAdmin account still has these credentials, change immediately as this is a well-known default pass).

0
votes

Have you tried using the swagger code generation tool as documented in the Cherwell documentation?

Once you have generated the client code, you will have wrapper data structures for all Cherwell REST API requests and responses.

Using Swagger Code Gen

You will need to install Maven and the Java Development kit.