2014-10-08 100 views
1

我想使用Ruby gem RestClient通过REST URL访问Rational Team Concert(RTC)中的记录。我已经用其他服务器成功完成了这项工作当我直接在Chrome中使用REST URL时,我可以获得记录。但是,当我使用我的Ruby代码时,我找回包含一行的某个页面:Ruby RestClient访问Rational Team Concert(RTC)REST

net.jazz.ajax._appPath =“/ ccm0001001/auth/authrequired”;

我试过各种各样的方式来传递证书,但似乎没有任何工作。这就是我想要的内容:

response = RestClient::Request.new(
    :method => :get, 
    :url => uri, 
    :user => "username", 
    :password => "password", 
    :verify_ssl => OpenSSL::SSL::VERIFY_NONE 
).execute 

有谁使用Ruby(或Python?)在REST的URL访问RTC,或有任何想法,我做错了什么?

谢谢!

回答

1

不知道你还在这里,但对于任何人来说,这可能会有所帮助。

默认情况下,第一个Rational Team Concert(RTC)和其他基于Jazz的产品默认默认使用基于表单的身份验证,Jazz管理员可以更改此设置以支持标准基本身份验证或更复杂的方法,如基于证书的身份验证卡片等TN0013: Jazz Team Server Authentication Explained。当您尝试通过浏览器登录时,很容易判断您是否具有表单身份验证或基本身份验证,然后将您导向登录servlet页面或提示输入操作系统/浏览器对话框

表单身份验证重定向至登录页面,如下所示: enter image description here

基本认证通过浏览器提示: enter image description here

我会使用Python和请求模块来简化代码显示了一些例子,这里是全球领先的锅炉板是继续下面的代码片段。

import requests 
# quiet warnings for self-signed certificates 
requests.packages.urllib3.disable_warnings() 

base_url = "https://jazzserver.demo.com/ccm" 
auth_uri = "/authenticated/identity" 

jazz_user = "user" 
jazz_pass = "secretpassword" 

session = requests.Session() 
session.verify = False 
session.allow_redirects = True 
session.headers = {'accept':'application/xml'} 

如果管理员启用基本身份验证,那么你所提供的例子就足以作为应该已经在你的会话,当你请求一个受保护的资源的凭据将提供与您的请求将会成功所需的认证。

session.auth = (jazz_user, jazz_pass) 

# Request a protected resource, auth creds are stored in session 
response = session.get(base_url + auth_uri) 

由于上面的代码片段不起作用,我假设您使用的是表单身份验证。在这种情况下,我们需要执行请求受保护资源的标准表单auth握手,遵循重定向到表单,使用提供的会话ID发布凭证以形成表单,验证我们是否已登录,并遵循重定向或重试受保护资源。

print "Request for authenticated resource" 
response = session.get(base_url + auth_uri) 

#print response.text 
print response.headers 

if 'x-com-ibm-team-repository-web-auth-msg' in response.headers and response.headers['x-com-ibm-team-repository-web-auth-msg'] == 'authrequired': 
    print "Not currently authenticated" 

    # Form response 
    print "Sending login POST" 
    login_response = session.post(base_url + '/j_security_check', data={ 'j_username': jazz_user, 'j_password': jazz_pass }) 
    print login_response.headers 

    if 'x-com-ibm-team-repository-web-auth-msg' in login_response.headers and login_response.headers['x-com-ibm-team-repository-web-auth-msg'] == 'authrequired': 
     print "Failed to authenticate" 
     print login_response.status_code 
     print login_response.text 
     raise Exception("Failed to login: ", login_response.text) 

    print "Getting authenticated resource again now that we should be logged in:" 
    response = session.get(base_url + auth_uri) 

print response.headers 
print response.text 

我还没有访问基于证书的认证服务器或有一组,所以我没有任何使用API​​的经验。

发布一个完整的例子,应为基本或窗体身份验证的GitHub的要点

Gist - rtc_request_example.py

应该给你这样的输出工作:

(rtc_client)[email protected]:~/workspaces/rtc_helper$ ./test_ccm.py 
    Request for authenticated resource 
    {'x-com-ibm-team-repository-web-auth-msg': 'authrequired', 'content-length': '1985', 'set-cookie': 'JSESSIONID=CEF68A74B1A8005EB91A90BA42F3F86A; Path=/ccm/; Secure; HttpOnly, JazzFormAuth=Form; Path=/ccm; Secure', 'expires': 'Wed, 31 Dec 1969 18:00:00 CST', 'server': 'Apache-Coyote/1.1', 'cache-control': 'private', 'date': 'Thu, 15 Jan 2015 18:43:35 GMT', 'content-type': 'text/html;charset=UTF-8'} 
    Not currently authenticated 
    Sending login POST 
    {'content-length': '55', 'set-cookie': 'JSESSIONID=1FE94776634391C83E47210113D1A4D4; Path=/ccm/; Secure; HttpOnly, JSESSIONIDSSO=D91E3A4E3376D567AF93DD031ED48E72; Path=/; Secure; HttpOnly, X-com-ibm-team-foundation-auth-loop-avoidance=false; Secure', 'expires': 'Wed, 31 Dec 1969 18:00:00 CST', 'server': 'Apache-Coyote/1.1', 'cache-control': 'private', 'date': 'Thu, 15 Jan 2015 18:43:36 GMT', 'content-type': 'text/json;charset=utf-8'} 
    Getting authenticated resource again now that we should be logged in: 
    {'content-length': '55', 'set-cookie': 'X-com-ibm-team-foundation-auth-loop-avoidance=false; Secure', 'expires': 'Wed, 31 Dec 1969 18:00:00 CST', 'server': 'Apache-Coyote/1.1', 'cache-control': 'private', 'date': 'Thu, 15 Jan 2015 18:43:36 GMT', 'content-type': 'text/json;charset=utf-8'} 
    { 
     "userId": "sgwilbur", 
     "roles": [ 
      "JazzAdmins"] 
    } 

-Sean

+0

感谢您的回复!我还没有尝试过实施你的解决方案,但为防万一我不尽快回复,我确实做了一些工作:我欺骗了!我使用Watir来控制Chrome浏览器。我知道这是一种黑客攻击,但它让我通过概念证明来展示什么是可能的。 – oaklodge 2015-01-19 09:03:21

+1

是的,这可能有点难以开始,最简单的东西,我只是推荐使用卷曲方法。 https://jazz.net/wiki/bin/view/Main/ResourceOrientedWorkItemAPIv2 – Sean 2015-01-19 15:42:47

相关问题