简体   繁体   English

Python / Rest中的身份验证问题

[英]Authentication Issue in Python/Rest

My issue is that I keep getting a "Response [401]" which I from what I have seen online means there is an authentication error. 我的问题是,我不断收到“响应[401]”,从网上看到的内容来看,这意味着存在验证错误。 I am trying to create a new user via Rest calls in python. 我正在尝试通过python中的Rest调用创建一个新用户。 I am quite new to Python/Rest so I am sure there is something I am missing. 我对Python / Rest相当陌生,因此我确定我缺少一些东西。

Below is my code: 下面是我的代码:

import requests
import json
import base64

print("Script Running")
username = ‘UN’
password = ‘PW’
tempString = username + ':' + password
encodedString = base64.b64encode(tempString.encode())
data = {"name": "changeUser", "password": "12345","emailAddress": "emailaddress@some.com","displayName": "changeUser","notification" : "false"}
url = "http://myserver.atlassian.net/rest/api/2/user/"
headers = {'Authentication': 'Basic ' + tempString, 'Content-type': 'application/json', 'Accept': 'application/json', 'X-Atlassian-Token': 'no-check' }

print(encodedString)
r = requests.post(url, json.dumps(data), headers)
print(r)
print("Script Ending")

encodedString returns b'stringofstuff=' print(r) returns Response[401] encodeString返回b'stringofstuff ='print(r)返回Response [401]

requests has options for all of this. 请求包含所有这些选项。 I'd just use 我只是用

r = requests.post(url, json=data, auth=(username, password))

Have you used a separate HTTP client to make this call? 您是否使用了单独的HTTP客户端进行此调用? Try using Postman to make the call using the same data so you can determine where to start looking first. 尝试使用Postman使用相同的数据拨打电话,以便您可以确定从哪里开始寻找。

Also - What exactly are you supposed to be doing with encodedString? 另外-您到底应该对encodeString做些什么? The only time you use it is in the print call. 您只有在打印呼叫中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM