Python request.session 存在 "You do not have the permission to see the specified issue"

Python request.session JIRA "You do not have the permission to see the specified issue"

首先,我知道这里介绍的解决方案: ... As well the linked 博客 post( @Nick Josevski 撰写),虽然有用,但没有'解决我的特定问题,这可能是微不足道的...

使用以下 Python 2.7.3 代码...

import requests
import getpass
import json

jira_user = raw_input("Username: ")
jira_pass = getpass.getpass()

session = requests.Session()
session.verify = jira_ca_certs # Our internal certs
auth_info = {"username": jira_user, "password":  jira_pass}
login_url = 'http://JIRA_SERVER.com/login.jsp'
session.post(login_url, data=auth_info)

我生成 cookies post 到 jira 的基本身份验证(注意:我正在使用 "http" 没有指定端口以通过登录页面进行身份验证 ) .. 由于 session 自动保存返回的 cookie,我可以使用 session.cookies 来设置 header:

cookies = requests.utils.dict_from_cookiejar(session.cookies)
headers = {'Content-type': 'application/json', 'cookie': cookies}

接下来,我使用 https + PORT[=55= 对捕获的 cookie 进行了安全 JIRA url 的基本访问测试]:

base = session.get('https://JIRA_SERVER.com:1234', headers=headers)
print 'base: ', base

以上,正如预期的那样,returns(虽然这可能不是一个有效的测试?)...

base:  <Response [200]>

现在测试代码的预期目的。我使用相同的方法针对特定的 JIRA 问题扩展 url:

jira = session.get('https://JIRA_SERVER.com:1234/rest/api/latest/issue/KRYP-6207', headers=headers)
print 'issue: ', jira
print jira.json()

对于 JSON 输出,我得到一个响应,说明我 没有权限 :

issue:  <Response [401]>
{u'errorMessages': [u'You do not have the permission to see the specified issue.', u'Login Required'], u'errors': {}}

我在 header 中使用的返回的 cookie 是:

headers:  {'cookie': 'atlassian.xsrf.token=XXXXXXXXXXXXXXXX|lout; Path=/, JSESSIONID=XXXXXXXXXXXXXXXX; Path=/'}

我不知道 为什么 这适用于 base url,但不适用于 issue url.. 我已经使用 Chrome POSTMAN 检查返回的 cookie,它们与上面列出的相同,即 atlassian.xsrf.token,和 JSESSIONID.

希望这里有人能告诉我我做错了什么!提前致谢...

可能是您没有正确登录?根据我在 webbrowser 开发人员工具中看到的请求,我创建了以下请求并且它与我们的 JIRA 一起工作:

import requests
import getpass
import json

jira_user = raw_input("Username: ")
jira_pass = getpass.getpass()

session = requests.Session()

headers = {
    'Host': 'our.jira.com',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://our.jira.com/login.jsp',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
}

data = [
  ('os_username', jira_user),
  ('os_password', jira_pass),
  ('os_destination', ''),
  ('user_role', ''),
  ('atl_token', ''),
  ('login', 'Anmelden'),
]

loginPost = session.post('https://our.jira.com/login.jsp', headers=headers, data=data)

xsrf_token = session.cookies.get_dict()['atlassian.xsrf.token']
jsessionid = session.cookies.get_dict()['JSESSIONID']



cookies = {
    'atlassian.xsrf.token': xsrf_token,
    'JSESSIONID': jsessionid,
}

headers = {
    'Host': 'our.jira.com',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
}

r = session.get('https://our.jira.com/rest/api/latest/issue/SDN-206', headers=headers, cookies=cookies)
print r.json()