Base64 编码问题 - Python vs Powershell
Base64 Encoding issue - Python vs Powershell
我正在尝试对身份验证令牌进行编码并将其传递给 REST API,这适用于 powershell,但将相同的方法应用于 python 脚本会引发 'unauthorized' 异常。
我怀疑编码值有问题。无法找出解决方案。有什么想法吗?
其余端点是 IBM uDeploy。
Powershell
$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( "PasswordIsAuthToken:{`"token`":`"$pass`"}" ))
$basicAuthValue = "Basic $tokenEncoded"
$headers = @{}
$headers.Add("Authorization", $basicAuthValue)
$response = Invoke-RestMethod -Method Put -Headers $headers -Uri $requestUri -Body $jsonRequest
Python
epass = base64.b64encode("PasswordIsAuthToken:{\"token\":\"$password\"}")
print 'base64 encoded: ' + epass
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(reqUrl,json.dumps(json_data))
req.add_header('Authorization', 'Basic '+epass)
req.get_method = lambda: 'PUT'
resp = opener.open(req)
您正在发送文字字符串 $password
作为标记,而不是名为 password
.
的变量的内容
您只需在基本身份验证 HTTP header 中包含 PasswordIsAuthToken
和您的令牌(PasswordIsAuthToken
构成用户名,token
构成密码):
epass = base64.b64encode("PasswordIsAuthToken:" + password)
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(reqUrl,json.dumps(json_data))
req.add_header('Authorization', 'Basic ' + epass)
如果您确实需要将令牌包装在 JSON-like 结构中,那么您需要使用字符串格式(Powershell 代码也这样做,但您省略了):
epass = base64.b64encode('PasswordIsAuthToken:{"token":"%s"}' % password)
或者您可以使用 json
模块:
epass = base64.b64encode('PasswordIsAuthToken:' + json.dumps({'token': password}))
但是,我相信系统应该接受解包的令牌。
我强烈建议您改用 requests
library,这使得使用 REST API 变得更加干净:
import requests
auth = ('PasswordIsAuthToken', password)
# or alternatively
# auth = ('PasswordIsAuthToken', '{"token":"%s"}' % password)
response = requests.put(json=json_data, auth=auth)
请注意,您无需自己对 JSON body 进行编码,也不必对 Basic Auth header.
进行编码
我正在尝试对身份验证令牌进行编码并将其传递给 REST API,这适用于 powershell,但将相同的方法应用于 python 脚本会引发 'unauthorized' 异常。
我怀疑编码值有问题。无法找出解决方案。有什么想法吗?
其余端点是 IBM uDeploy。
Powershell
$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( "PasswordIsAuthToken:{`"token`":`"$pass`"}" ))
$basicAuthValue = "Basic $tokenEncoded"
$headers = @{}
$headers.Add("Authorization", $basicAuthValue)
$response = Invoke-RestMethod -Method Put -Headers $headers -Uri $requestUri -Body $jsonRequest
Python
epass = base64.b64encode("PasswordIsAuthToken:{\"token\":\"$password\"}")
print 'base64 encoded: ' + epass
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(reqUrl,json.dumps(json_data))
req.add_header('Authorization', 'Basic '+epass)
req.get_method = lambda: 'PUT'
resp = opener.open(req)
您正在发送文字字符串 $password
作为标记,而不是名为 password
.
您只需在基本身份验证 HTTP header 中包含 PasswordIsAuthToken
和您的令牌(PasswordIsAuthToken
构成用户名,token
构成密码):
epass = base64.b64encode("PasswordIsAuthToken:" + password)
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(reqUrl,json.dumps(json_data))
req.add_header('Authorization', 'Basic ' + epass)
如果您确实需要将令牌包装在 JSON-like 结构中,那么您需要使用字符串格式(Powershell 代码也这样做,但您省略了):
epass = base64.b64encode('PasswordIsAuthToken:{"token":"%s"}' % password)
或者您可以使用 json
模块:
epass = base64.b64encode('PasswordIsAuthToken:' + json.dumps({'token': password}))
但是,我相信系统应该接受解包的令牌。
我强烈建议您改用 requests
library,这使得使用 REST API 变得更加干净:
import requests
auth = ('PasswordIsAuthToken', password)
# or alternatively
# auth = ('PasswordIsAuthToken', '{"token":"%s"}' % password)
response = requests.put(json=json_data, auth=auth)
请注意,您无需自己对 JSON body 进行编码,也不必对 Basic Auth header.
进行编码