如何使用REST API登录IBM Bluemix?

时间:2017-02-07 17:46:04

标签: go ibm-cloud cloudfoundry

我正在尝试使用Cloud Foundry go-cfclient在Go中使用IBM Bluemix和REST API。我已经失败了登录过程。我正在使用以下示例代码并通过传入Bluemix端点来调用该程序" https://api.ng.bluemix.net"和我的用户名/密码信息。

package main

import (
    "flag"
    "fmt"
    "os"

    cfclient "github.com/cloudfoundry-community/go-cfclient"
)

func main() {
    api := flag.String("api", "", "API endpoint")
    username := flag.String("username", "", "User name")
    password := flag.String("password", "", "password")
    help := flag.Bool("help", false, "help")

    flag.Parse()

    if *help || len(*api) == 0 || len(*username) == 0 || len(*password) == 0 {
        flag.Usage()
        os.Exit(1)
    }

    config := &cfclient.Config{
        ApiAddress: *api,
        Username:   *username,
        Password:   *password}

    fmt.Println("user %v\n",*username)
    var (
        client *cfclient.Client
        err    error
    )

    if client, err = cfclient.NewClient(config); err != nil {
        panic(err)
    }
    fmt.Println(client)

    apps, err := client.ListApps()

    if err != nil {
        panic(err)
    }

    fmt.Println(apps)
}

返回的错误是:

  

恐慌:获取令牌时出错:oauth2:无法获取令牌:401   未经授权的回复:   {"错误":"未经授权"," error_description":"凭据错误"}

需要提供哪些信息?如何使用REST API登录Bluemix?

1 个答案:

答案 0 :(得分:1)

Here is an example使用REST API(在JavaScript中)登录Bluemix。

您拨打登录端点并使用Bluemix中的用户名和密码请求令牌。

body of request

headers of request