SoftLayer_User_Customer_ApiAuthentication :: getUser因异常而失败

时间:2018-05-22 21:50:10

标签: api ibm-cloud-infrastructure

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"  // used actual username and api-key
            apikey := "My-APIkey"

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

            // Get SoftLayer_Account service
            resp := services.GetUserCustomerApiAuthenticationService(sess)
            users, err := resp.GetUser()
            if err != nil {
                    fmt.Printf("\n Unable to get users:\n - %s\n", err)
                    return
            }
            fmt.Printf("\n Unable to get users:\n - %s\n", users)
    }

===== $ ./list_user

无法吸引用户:

- SoftLayer_Exception:执行方法时不存在对象。 (SoftLayer_User_Customer_ApiAuthentication :: getUser)(HTTP 500)

我尝试了类似的代码来获取对象。它抛出相同的异常。

1 个答案:

答案 0 :(得分:0)

您收到此错误的原因是两个方法都需要使用User_Customer_ApiAuthentication标识符。

您可以在两种方法上使用以下代码,因为它们将使用相同的身份验证ID:

package main

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

  )

  func main() {
        // SoftLayer API username and key
        username := "my-username"  // used actual username and api-key
        apikey := "My-ApiKey"

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

        // Get SoftLayer_Account service
        resp := services.GetUserCustomerApiAuthenticationService(sess)
        users, err := resp.Id(123456).GetUser()
        if err != nil {
                fmt.Printf("\n Unable to get users:\n - %s\n", err)
                return
        }
        //Following helps to print the result in json format.
        jsonFormat, jsonErr := json.MarshalIndent(users,"","     ")
        if jsonErr != nil {
               fmt.Println(jsonErr)
               return
        }
        fmt.Println(string(jsonFormat))
}

如果您需要检索上面的身份验证ID,可以使用SoftLayer_User_Customer :: getObject方法并搜索将包含它的 apiAuthenticationKeys 关系属性。

相关问题