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

调用SoftLayer_User_Customer_ApiAuthentication::getUser遇HTTP 500异常求助

解决SoftLayer_User_Customer_ApiAuthentication::getUser的"Object does not exist"异常

这个错误的核心原因是:你调用的getUser是SoftLayer_User_Customer_ApiAuthentication的实例方法,而当前的服务实例没有绑定到一个有效的ApiAuthentication对象上。SoftLayer的这类服务需要先定位到具体的对象实例(通过ID),才能调用其实例方法,否则就会抛出"对象不存在"的500异常。

解决方案1:先获取ApiAuthentication对象ID,再绑定调用

你需要先通过用户服务获取到对应的API认证密钥记录,拿到ID后再初始化ApiAuthentication服务实例,然后调用getUser:

package main

import (
	"fmt"
	"github.com/softlayer/softlayer-go/services"
	"github.com/softlayer/softlayer-go/session"
)

func main() {
	// SoftLayer API username and key
	username := "my-username"
	apikey := "My-APIkey"

	// Create SoftLayer API session
	sess := session.New(username, apikey)

	// 1. 先获取当前用户的API认证密钥列表
	userService := services.GetUserCustomerService(sess)
	authKeys, err := userService.GetCurrentUser().GetApiAuthenticationKeys()
	if err != nil {
		fmt.Printf("\nUnable to get API authentication keys:\n - %s\n", err)
		return
	}

	if len(authKeys) == 0 {
		fmt.Println("\nNo API authentication keys found for the current user.")
		return
	}

	// 2. 取第一个认证密钥,绑定到ApiAuthentication服务
	firstAuthKeyId := *authKeys[0].Id
	authService := services.GetUserCustomerApiAuthenticationService(sess).Id(firstAuthKeyId)

	// 3. 调用getUser方法
	user, err := authService.GetUser()
	if err != nil {
		fmt.Printf("\nUnable to get user from API authentication key:\n - %s\n", err)
		return
	}

	fmt.Printf("\nFetched user details:\n Username: %s\n Email: %s\n", *user.Username, *user.Email)
}

解决方案2:直接获取当前用户(如果这是你的需求)

如果你只是想获取当前调用API的用户信息,完全不需要通过ApiAuthentication服务,直接用GetCurrentUser方法更简单:

package main

import (
	"fmt"
	"github.com/softlayer/softlayer-go/services"
	"github.com/softlayer/softlayer-go/session"
)

func main() {
	username := "my-username"
	apikey := "My-APIkey"

	sess := session.New(username, apikey)
	userService := services.GetUserCustomerService(sess)

	user, err := userService.GetCurrentUser()
	if err != nil {
		fmt.Printf("\nUnable to get current user:\n - %s\n", err)
		return
	}

	fmt.Printf("\nCurrent user details:\n Username: %s\n Email: %s\n", *user.Username, *user.Email)
}

额外排查点

  • 确认你的API密钥对应的用户确实存在API认证记录(可以通过SoftLayer控制台查看用户的API密钥)。
  • 检查你的API密钥是否有足够的权限访问用户相关的服务(比如User_Customer和User_Customer_ApiAuthentication的权限)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:34:52