Skip to content

获取用户头像

用于获取指定用户的头像图片,支持GET请求方式。需传递应用ID和用户名参数,接口直接返回头像图片二进制数据(JPEG格式),或返回错误信息。

请求地址 GET

http
https://nobase.cn/api/user/get_avatar

请求参数

参数名位置类型必选说明
appidquerystring✅ 是应用ID,填写从开发者中心获取的应用唯一标识
userquerystring✅ 是用户名,需为该应用下已注册的用户账号(对应ye_users表的username字段)

返回说明

  • 成功响应:返回image/jpeg格式的图片二进制数据,可直接用于图片展示(如<img>标签引用)。

  • 失败响应:返回JSON格式的错误信息,示例如下:

    json
    {
      "code": 204,
      "msg": "接口参数错误"
    }
    json
    {
      "code": 404,
      "msg": "用户不存在"
    }

返回数据结构(失败时)

参数名类型说明
codenumber状态码:204=参数缺失/错误;404=用户不存在或头像信息缺失
msgstring错误描述:如“接口参数错误”“用户不存在”

代码示例

javascript
// 1. 配置参数
const appid = "your_app_id";
const user = "13800138000"; // 目标用户账号

// 2. 拼接请求URL
const avatarUrl = `https://nobase.cn/api/user/get_avatar?appid=${appid}&user=${user}`;

// 3. 在页面中展示头像
const imgElement = document.createElement("img");
imgElement.src = avatarUrl; // 直接将接口地址作为图片源
imgElement.alt = "用户头像";
document.body.appendChild(imgElement);

// 处理图片加载失败
imgElement.onerror = () => {
  console.error("头像加载失败,可能是参数错误或用户不存在");
};
python
import requests

# 1. 配置参数
appid = "your_app_id"
user = "13900139000"
url = f"https://nobase.cn/api/user/get_avatar?appid={appid}&user={user}"

# 2. 发起请求并保存图片
try:
    response = requests.get(url, timeout=10)
    # 检查响应是否为图片(成功时Content-Type为image/jpeg)
    if response.headers.get("Content-Type") == "image/jpeg":
        with open("user_avatar.jpg", "wb") as f:
            f.write(response.content)
        print("头像下载成功,已保存为user_avatar.jpg")
    else:
        # 失败时返回JSON错误信息
        error_info = response.json()
        print(f"获取失败:{error_info['msg']}")
except requests.exceptions.RequestException as e:
    print(f"请求异常:{str(e)}")

注意事项

  1. 参数必填性appiduser为必填参数,缺少任一参数会返回204错误(接口参数错误)。
  2. 用户存在性:若user对应的用户在ye_users表中不存在,或该用户未设置头像(icon字段为空/无效),接口可能返回404错误或图片加载失败。
  3. 响应类型:成功时响应头Content-Typeimage/jpeg,直接返回图片二进制数据,可直接作为图片资源引用(如<img>标签的src属性);失败时返回JSON格式错误信息。
  4. 使用场景:适用于前端展示用户头像、后端批量获取用户头像等场景,建议前端添加图片加载失败的兜底显示(如默认头像)。
  5. 权限说明:接口未验证应用签名或用户令牌,仅通过appiduser匹配用户,需确保appiduser的合法性(避免恶意请求不存在的用户)。