2017-02-02 100 views
2

我想在使用OAUTH2身份驗證的api上使用RequestsLibrary。使用OAUTH2的Robot Framework API測試授權請求標頭

身份驗證通過OAUTH2提供給憑證提供給/ v1/authtoken端點。 後續對API的調用需要在http請求的「授權」標頭中將令牌包含爲「承載者」。

下面是測試用例。我得到的錯誤是: 401!= 200

憑據在jmeter中正常工作,並返回一個帳戶列表。但是,我無法獲得RF腳本的工作。任何幫助將不勝感激。

在腳本中,

  1. 登錄到控制檯${accessToken}返回訪問令牌:8ETFXTZOWQLrgsMj7c_KuCEeypdj-eO1r ...
  2. 登錄到控制檯${token}回報:承載8ETFXTZOWQLrgsMj7c_KuCEeypdj-eO1r ...


*** Test Cases *** 

Get authToken 
    Create Session hook http://xxxx.azurewebsites.net verify=${True} 
    ${data}=  Create Dictionary  grant_type=client_credentials  client_id=yyy-zzzz  client_secret=xxxxxxxxxxxxxxx 
    ${headers}= Create Dictionary  Content-Type=application/x-www-form-urlencoded 
    ${resp}= post request hook /v1/authtoken data=${data} headers=${headers} 
    Should Be Equal As Strings ${resp.status_code}  200 
    Dictionary Should Contain Value  ${resp.json()} bearer 
    ${accessToken}= evaluate $resp.json().get("access_token") 
    Log to Console  ${accessToken} 
    ${Bearer}=  Set Variable Bearer 
    ${token}=  catenate Bearer ${accessToken} 
    Log to Console  ${token} 
    ${headers}= Create Dictionary Authorization=${token} 
    ${resp1}=  get request  hook /v1/integration/accounts headers=${headers} 
    Should Be Equal As Strings ${resp1.status_code} 200 
    #Log to Console ${resp1.json()} 
+0

@桑迪普@Bryan你找到問題的答案,因爲我得到同樣的錯誤。好心分享答案 –

+0

對不起,我不在。剛剛看到這個討論....我會試試這個。 –

+0

是的,這現在似乎適用於我。謝謝。 但是,我純粹的生成auth2令牌的python方法是,我必須說得更簡潔! –

回答

0

我正在使用OAuth 2.0身份驗證以及我的salesforce自動化。

我的第一個答案會跳過基於客戶端的認證,並切換到基於用戶名/口令認證

 Get authToken by Password Authentication 

RequestsLibrary.Create Session hook https://<url>/services/oauth2 verify=${True} 
${data}=  Create Dictionary  grant_type=password  client_id=1abc client_secret=2abc [email protected] password=keypass 
${headers}= Create Dictionary  Content-Type=application/x-www-form-urlencoded 
${resp}=  RequestsLibrary.Post Request hook /token data=${data} headers=${headers} 
Should Be Equal As Strings ${resp.status_code}  200 
${accessToken}= evaluate $resp.json().get("access_token") 
Log to Console  ${accessToken} 

如果使用的是基於客戶端或基於Web認證,會出現一個登錄界面即會被用戶用來輸入他們的用戶名/密碼來授權應用程序代表它發送請求。查看這些頁面以獲取更多信息,因爲它們主要討論如何使用刷新標記或完全跳過用戶提示。

相關問題