【问题标题】:Access Sonarqube Webapi with python requests使用 python 请求访问 Sonarqube Webapi
【发布时间】:2018-06-25 04:16:46
【问题描述】:

我对@9​​87654322@ 很陌生。我只是尝试一种简单的方法来使用 python 获取 HTTP 响应,以从声纳 Web API 中简单获取

我使用请求库并尝试简单的使用:

project = requests.get(url=Sonar_Api_Projects_Search, params=param_Projects, verify=False, headers={'Authorization': 'token {}'.format(token)})

请求格式正确,当我在网络浏览器中使用它时可以正常工作。

但作为回应,我得到了这个奇怪的输出:

{"err_code":500,"err_msg":"未定义方法empty?' for nil:NilClass\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:132:in login_from_basic_auth'\n\torg/jruby/RubyProc.java:290:in call'\n\torg/jruby/RubyProc.java:224:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/http_authentication.rb:126:in authenticate'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/http_authentication.rb:116:in authenticate_with_http_basic'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:129:in login_from_basic_auth'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:11:in current_user'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/app/controllers/application_controller.rb:102:in set_user_session'\n\torg/jruby/RubyKernel.java:2223:in send'\n\tD:/sonarqube-5.6.6_20170214 /web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/callbacks.rb:178:in evaluate_method'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/callbacks.rb:166:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:225:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:629:in run_before_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:615:in call_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:610:in perform_action_with_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/benchmarking.rb:68:in perform_action_with_benchmark'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/core_ext/benchmark.rb:17:in ms'\n\tjar:file:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-complete-1.7.9.jar!/META-INF/jruby.home/lib/ruby/1.8 /benchmark.rb:308:in realtime'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/core_ext/benchmark.rb:17:in ms'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/benchmarking.rb:68:in perform_action_with_benchmark'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/rescue.rb:160:in perform_action_with_rescue'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/flash.rb:151:in perform_action_with_flash'\n\torg/jruby/RubyKernel.java:2223:in send'\ n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/base.rb:532:in process'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:606:in process_with_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/base.rb:391:in process'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/base.rb:386:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/routing/route_set.rb:450:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:87:in dispatch'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:85:in dispatch'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:121:in _call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:130:in build_middleware_stack'\n\torg/jruby/RubyProc.java:290:in call'\n\torg/jruby/RubyProc.java:224:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:29:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in cache'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:9:in cache'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:28:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/config/environment.rb:67:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/string_coercion.rb:25:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/rack-1.1.6/lib/rack/head.rb:9:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/rack-1.1.6/lib/rack/methodoverride.rb:24:in call'\ n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/params_parser.rb:15:in call'\n\tfile:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-rack-1.1.13.2.jar!/jruby/rack/session_store.rb:70:in context'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/rack-1.1.6/lib/rack/session/abstract/id.rb:58:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/failsafe.rb:26:in call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:106:in call'\n\tfile:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-rack-1.1.13.2.jar!/rack/adapter/rails.rb:34:in serve_rails'\n\tfile:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-rack-1.1.13.2.jar!/rack/adapter/rails.rb:39:in call'\n\tfile:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-rack-1.1.13.2.jar!/rack/handler/servlet.rb:22:in call'\n"}

有人可以帮我吗?

非常感谢

最好的问候

阿诺

【问题讨论】:

  • 您应该首先考虑升级到最新的 LTS,6.7。
  • 不幸的是,我不负责 SonarQube 的安装。这是 5.6.6 版本的已知问题吗?顺便说一句,当我使用 .Net 代码执行相同的代码时,它可以工作。
  • 问题来自身份验证。你确定 "headers={'Authorization': 'token {}'.format(token)}" 吗?
  • 我在这个对象中尝试的一切都会产生相同的结果(如果我禁止标记,如果我写的是基本而不是标记)。其实我什么都不确定。我只知道如果我取消授权,我会收到 401 响应(这是合乎逻辑的)。
  • 我不知道 Python 请求库是如何使用基本身份验证的,您应该查看文档,例如查看来自 google 的第一个结果:docs.python-requests.org/en/master/user/authentication

标签: python sonarqube python-requests


【解决方案1】:

直接使用请求对我没有用。

我执行以下操作,并且工作正常: (下面的代码是列出Sonar中的项目)

import json , requests, pprint

url = 'http://sonar_url:9000/api/projects/search'
myToken = 'fa2377941a95125443f4efade615512jjkd221211a48'

session = requests.Session()
session.auth = myToken, ''

call = getattr(session, 'get')
res = call(url)
print(res.status_code)

binary = res.content
output = json.loads(binary)
pprint.pprint(output)
...
#Parse json result

【讨论】:

  • 请注意,并非所有 SonarQube 安装都在侦听端口 9000... 我使用了一些在 80 和 443 上侦听的;如果请求停止,请尝试这些。
  • 知道为什么创建会话有效,但不能直接调用请求的 http 方法吗?我知道会话提供 cookie 持久性、连接池和配置,但是 SonarQube 的 api 看到这些东西并允许直接使用请求吗?
【解决方案2】:

在 Sonarqube 8.9 中,requests 为我工作。

首先,您应该创建一个 API 令牌。根据文档:

这是推荐的方式。好处在页面用户令牌中描述。令牌通过 HTTP 基本认证的登录字段发送,无需任何密码。

文档继续提供了一个奇怪的 curl 用法示例:

# note that the colon after the token is required in curl to set an empty password 
curl -u THIS_IS_MY_TOKEN: https://sonarqube.com/api/user_tokens/search

requests 中,这看起来像这样:

response = requests.get(
    "http://your-sonar-instance.com/api/blah",
    auth=HTTPBasicAuth("Some Sonarqube API token", "")
)

return json.loads(response.text)

有关 API 详细信息,请参阅 https://docs.sonarqube.org/latest/extend/web-api/

另请注意,auth=HTTPBasicAuth("token", "") 的行为似乎与 auth=HTTPBasicAuth("token", None) 不同。

【讨论】:

    【解决方案3】:

    我知道这是一个老问题。值得庆幸的是,现在有一个包装库可用 - https://github.com/shijl0925/python-sonarqube-api。它运行良好且易于设置。

    如果可能的话,Sonarsource 的人可以将其设为官方版本,以便更多的人开始使用它,并在未来得到维护。

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 1970-01-01
      • 2017-10-07
      • 2023-03-24
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多