RestSharp Oauth2认证Cherwell restful api

时间:2016-08-02 18:02:51

标签: api restsharp oauth2

我通常不会在这里提问,因为大多数时候我都能找到答案。但此刻我还没找到一个,我真的被卡住了。我在尝试获取Cherwell api的访问令牌时遇到问题:http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token我使用postman生成此代码:

这与Cherwell Service Management的V8 + REST API相关。

抛出服务器运行时异常的代码:

 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);

当我从swagger ui(http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token)执行相同的方法时,我可以获得令牌而不会出现任何错误。

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

请求网址 http://13.88.176.216/CherwellAPI/token?auth_mode=Internal

这是来自swagger ui测试的响应主体,而不是我的代码:

{
  "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"
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试将用户名/密码作为表单编码数据的一部分。

删除身份验证器部分,此部分不应该是必需的。

所以,

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);

我实际上刚刚在不久前录制了一个视频(使用浏览器休息客户端,而不是C#,但是你得到了图片),应该很快就会在https://youtube.com/beyond20llc发布到我们的youtube频道 - 我可以发送如果你想在它到达youtube之前看到这个视频给你。

我在验证令牌时发送的数据基本上如下所示:

grant_type=password&

client_id=1234567890&

username=CSDAdmin&

password=CSDAdmin

(当然,CSDAdmin是彻底安装Cherwell的默认用户名/密码 - 如果您的CSDAdmin帐户仍然拥有这些凭据,请立即更改,因为这是一个众所周知的默认传递。)

答案 1 :(得分:0)

您是否尝试过使用Cherwell文档中记录的swagger代码生成工具?

生成客户端代码后,您将拥有所有Cherwell REST API请求和响应的包装器数据结构。

Using Swagger Code Gen

您需要安装Maven和Java Development Kit。

相关问题