如何使用REST API登录IBM Bluemix?

huangapple go评论94阅读模式
英文:

How to log into IBM Bluemix using REST API?

问题

我正在使用Cloud Foundry的go-cfclient库与IBM Bluemix和Go中的REST API进行交互。但是在登录过程中遇到了问题。我正在使用以下示例代码,并通过传入Bluemix的端点"https://api.ng.bluemix.net"以及我的用户ID/密码信息来调用程序。

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)
}

返回的错误是:

panic: Error getting token: oauth2: cannot fetch token: 401
Unauthorized Response:
{"error":"unauthorized","error_description":"Bad credentials"}

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

英文:

I am trying to use the Cloud Foundry go-cfclient to work with IBM Bluemix and the REST API in Go. I already fail with the login process. I am using the following sample code and invoke the program by passing in the Bluemix endpoint "https://api.ng.bluemix.net" and my userid/password info.

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)
}

The error returned is:

> panic: Error getting token: oauth2: cannot fetch token: 401
> Unauthorized Response:
> {"error":"unauthorized","error_description":"Bad credentials"}

What information needs to be provided? How can I log into Bluemix using the REST API?

答案1

得分: 1

以下是使用REST API登录Bluemix的示例(使用JavaScript)。

您可以调用登录端点,并使用您的Bluemix用户名和密码请求令牌。

请求的主体如下所示:

如何使用REST API登录IBM Bluemix?

请求的头部如下所示:

如何使用REST API登录IBM Bluemix?

英文:

Here is an example of logging into Bluemix using the REST API (in JavaScript).

You make a call to the login endpoint and request a token with your username and password from Bluemix.

如何使用REST API登录IBM Bluemix?

如何使用REST API登录IBM Bluemix?

huangapple
  • 本文由 发表于 2017年2月8日 01:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/42096526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定