调用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
相关产品推荐
相关产品推荐

