Ruby RestClient 访问 Rational Team Concert (RTC) REST [英] Ruby RestClient to access Rational Team Concert (RTC) REST

查看:43
本文介绍了Ruby RestClient 访问 Rational Team Concert (RTC) REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Ruby gem RestClient 通过 REST URL 访问来自 Rational Team Concert (RTC) 的记录.我已经用其他服务器成功地做到了这一点.当我直接在Chrome中使用REST URL时,我可以得到一个记录.但是,当我使用我的 Ruby 代码时,我会返回一些包含一行的页面:

<块引用>

net.jazz.ajax._appPath = "/ccm0001001/auth/authrequired";

我尝试了各种方法来传递凭据,但似乎没有任何效果.这就是我想要使用的:

response = RestClient::Request.new(:方法 =>:得到,:url =>尿,:用户 =>用户名",:密码 =>密码",:verify_ssl =>OpenSSL::SSL::VERIFY_NONE).执行

有人曾经使用 Ruby(或 Python?)通过 REST URL 访问 RTC,或者知道我做错了什么?

谢谢!

解决方案

不确定您是否仍然被困在这里,但对于其他任何人,这可能会有所帮助.

First Rational Team Concert (RTC) 和其他基于 Jazz 的产品默认默认为基于表单的身份验证,这可以由 Jazz 管理员更改以支持标准的基本身份验证或更复杂的方法,例如用于通用访问卡的基于证书的身份验证等等

应该给你这样的输出:

 (rtc_client)sgwilbur@gura:~/workspaces/rtc_helper$ ./test_ccm.py请求经过身份验证的资源{'x-com-ibm-team-repository-web-auth-msg':'authrequired','content-length':'1985','set-cookie':'JSESSIONID=CEF68A74B1A8005EB91A90BA42F3F86A;路径=/ccm/;安全的;HttpOnly, JazzFormAuth=Form;路径=/ccm;Secure', 'expires': 'Wed, 31 Dec 1969 18:00:00 CST', 'server': 'Apache-Coyote/1.1', 'cache-control': 'private', 'date': 'Thu,2015 年 1 月 15 日 18:43:35 GMT', 'content-type': 'text/html;charset=UTF-8'}当前未通过身份验证发送登录POST{'内容长度':'55','设置cookie':'JSESSIONID=1FE94776634391C83E47210113D1A4D4;路径=/ccm/;安全的;HttpOnly,JSESSIONIDSSO=D91E3A4E3376D567AF93DD031ED48E72;路径=/;安全的;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,2015 年 1 月 15 日 18:43:36 GMT', 'content-type': 'text/json;charset=utf-8'}现在我们应该登录,再次获取经过身份验证的资源:{'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,2015 年 1 月 15 日 18:43:36 GMT', 'content-type': 'text/json;charset=utf-8'}{"userId": "sgwilbur",角色":[爵士管理员"]}

-肖恩

I want to use the Ruby gem RestClient to access records from Rational Team Concert (RTC) over REST URLs. I have done this successfully with other servers. When I use the REST URL in Chrome directly, I can get a record. However, when I use my Ruby code, I get back some page that includes a line:

net.jazz.ajax._appPath = "/ccm0001001/auth/authrequired";

I've tried all sorts of ways to pass the credentials, but nothing seems to work. This is what I want to use:

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

Anyone ever use Ruby (or Python?) to access RTC over REST URLs, or have any ideas what I'm doing wrong?

Thanks!

解决方案

Not sure you are still stuck here, but for anyone else this might help.

First Rational Team Concert(RTC) and the other Jazz based products default to Form based authentication by default, this can be changed by the Jazz Administrators to support the standard Basic authentication or more complex methods like certificate based authentication for common access cards and the like TN0013: Jazz Team Server Authentication Explained. It is easy to tell if you have Form or Basic authentication, when you try and login via the browser are you directed to a login servlet page or prompted for an operating system/browser dialog

Form Auth redirects to login page like this:

Basic Auth prompts via browser:

I'll show some examples using Python and the Requests module to simplify the code, here is the leading boiler plate that proceeds the below snippets.

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'}

If your administrator has activated Basic auth, then the example you provided would be sufficient as the authentication needed would already be in your session and when you requested a secured resource the credentials will be provided and your request will succeed.

session.auth = (jazz_user, jazz_pass)

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

Since your snippet above is not working, I would assume you are using Form authentication. In this case we need to do the standard form auth handshake of request a protected resource, follow redirect to form, post credentials to form with provided session id, verify we are logged in, and either follow redirect or retry protected resource.

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

I have not accessed a certificate based authentication server or have one set so I don't have any experience using the API against that yet.

Posted a full example that should work for either Basic or Form auth as a GitHub gist

Gist - rtc_request_example.py

Should give you an output like this:

    (rtc_client)sgwilbur@gura:~/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

这篇关于Ruby RestClient 访问 Rational Team Concert (RTC) REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆