2016-08-02 93 views
0

我通常不會在這裏提問,因爲大部分時間我都可以找到答案。但在這一刻,我沒有找到一個,我真的被卡住了。我有一個問題,試圖獲取Cherwell api的訪問令牌:http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token我使用郵遞員生成此代碼:RestSharp Oauth2認證Cherwell restful api

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

的事情是,當我從招搖UI(http://13.88.176.216/CherwellAPI/Swagger/ui/index#!/Service/Service_Token)執行相同的方法,我可以得到令牌沒有得到任何錯誤。在CURL請求

詳細說明:

捲曲

捲曲-X POST --header 「內容類型:應用程序/ x WWW的形式進行了urlencoded」 --header「接受:應用/ JSON」 -D 「grant_type =密碼& CLIENT_ID = my_client_id & client_secret = my_client_secret &用戶名= my_user_name &密碼= MY_PASSWORD」 「http://13.88.176.216/CherwellAPI/token?auth_mode=Internal

請求URL http://13.88.176.216/CherwellAPI/token?auth_mode=Internal

這是從招搖的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" 
} 

任何幫助將不勝感激。

回答

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#,但你得到的圖片),應該很快就發佈到我們的YouTube頻道在https://youtube.com/beyond20llc - 如果您希望在到達YouTube之前看到此視頻,我可以將此視頻發送給您。

,當我正在驗證一個令牌我發送的數據基本上看起來像下面:

grant_type=password& 

client_id=1234567890& 

username=CSDAdmin& 

password=CSDAdmin 

(當然,CSDAdmin是在全新安裝徹韋爾的默認用戶名/密碼 - 如果你CSDAdmin帳戶仍然具有這些憑據,因爲這是一個衆所周知的默認傳遞,所以立即更改)。

0

您是否嘗試過使用如Cherwell文檔中記錄的Swagger代碼生成工具?

生成客戶端代碼後,您將擁有所有Cherwell REST API請求和響應的包裝數據結構。

Using Swagger Code Gen

你需要安裝Maven和Java開發工具包。