You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BambooHR Python GET请求返回401错误 同逻辑Google Apps Script正常如何解决

问题根因

Bamboo HR的Basic认证规则要求:需要将API密钥 + :x拼接为字符串后做Base64编码,再放到Authorization请求头的Basic 后面。你当前的Python代码直接将明文API密钥放在了Authorization头中,没有完成拼接、编码步骤,因此触发401未授权错误。

修正方案

方案1:手动处理编码(和你正常运行的GAS代码逻辑完全对齐)

import requests
import base64

url = "https://api.bamboohr.com/api/gateway.php/companyDomain/v1/employees/directory"
api_key = "替换为你的实际API密钥"
# 按规则拼接后做Base64编码
auth_content = f"{api_key}:x"
base64_auth = base64.b64encode(auth_content.encode("utf-8")).decode("utf-8")

headers = {
    "Accept": "application/json",
    "Authorization": f"Basic {base64_auth}"
}

response = requests.get(url, headers=headers)
print(response.text)

方案2:使用requests内置auth参数(更简洁,无需手动处理编码)

requests库原生支持Basic认证,直接传入auth元组即可,框架会自动完成编码逻辑:

import requests

url = "https://api.bamboohr.com/api/gateway.php/companyDomain/v1/employees/directory"
api_key = "替换为你的实际API密钥"

headers = {
    "Accept": "application/json"
}

# 元组第一个参数为API密钥,第二个参数固定为'x'
response = requests.get(url, headers=headers, auth=(api_key, 'x'))
print(response.text)

内容的提问来源于stack exchange,提问作者Mayank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 06:48:03