goalng oauth2:无法获取令牌:“0”;客户机“id不能为空”;

goalng oauth2:无法获取令牌:“0”;客户机“id不能为空”;,go,oauth-2.0,Go,Oauth 2.0,我正在进行OAuth2身份验证,下面是它的代码片段 import ( "context" "fmt" "net/http" "golang.org/x/oauth2" ) var ( ctx context.Context conf = &oauth2.Config{ RedirectURL: "http://localhost:3001/ApiCallback", ClientID: "C1c

我正在进行OAuth2身份验证,下面是它的代码片段

import (
    "context"
    "fmt"
    "net/http"
    "golang.org/x/oauth2"
)

var (
    ctx  context.Context
    conf = &oauth2.Config{
        RedirectURL:  "http://localhost:3001/ApiCallback",
        ClientID:     "C1c179367105720f145aecefa4fafdd587f3",
        ClientSecret: "ff3abcfad5acfe03ff64e9ca56c636e72b12",
        Scopes:       []string{"all"},
        Endpoint: oauth2.Endpoint{AuthURL: "https://api.example.com/v1/authorize",
            TokenURL: "https://api.example.com/v1/access_token",
        },
    }
    url = conf.AuthCodeURL("state", oauth2.AccessTypeOnline)
)

func main() {

    ctx = context.Background()
    http.HandleFunc("/", handler)
    http.HandleFunc("/ApiLogin", handleOAuthAPILogin)
    http.HandleFunc("/ApiCallback", handleAPICallback)
    fmt.Println(http.ListenAndServe(":3001", nil))

    fmt.Printf("Visit the URL for the auth dialog: %v", url)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func handleOAuthAPILogin(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Came to Spark Login")
    http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func handleAPICallback(w http.ResponseWriter, r *http.Request) {

    fmt.Println("Came to Callback")
    code := r.FormValue("code")

    fmt.Println("Auth Code:", code)
    fmt.Println("Client ID:", conf.ClientID)
    fmt.Println("Client Secret:", conf.ClientSecret)

    token, err := conf.Exchange(ctx, code)
    if err != nil {
        fmt.Println("Code exchange failed with '%s'\n", err)
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
    }
    fmt.Println("Token Code:", token)
}
使用上面的代码片段, 我可以在登录后获取代码(授权代码),但在它交换代码时失败。它抛出的错误是“client_id”为null

我已经验证了我的“conf”对象,有值存在

但它仍然抛出了这个错误。谁能帮我修一下吗

这是输出

$ go run main.go
Came to Spark Login
Came to Callback
Auth Code: YzdjZGZmZTgtYTllYi00zQ1OTI2OTNiZTQtYmQx
Client ID: C1c179367145aecefa4fafdd587f3
Client Secret: ff3abcfad5acfe034b9531ff64e9ca56c636e72b12
Code exchange failed with '%s'
 oauth2: cannot fetch token: 400 Bad Request
Response: {"errorCode":0,"message":"client_id cannot be null","errors":[{"errorCode":0,"description":"client_id cannot be null"}],"trackingId":"NA_3ca9eb7e-f3ce-406
2-8c29-7030ea9b1a08"}

请在发布代码之前
fmt
您的代码。。。这对你来说是一种痛苦,我没有修改代码。它现在正在正确显示。您是否也这么看?OAuth服务器是否已“损坏”(即,它是否希望通过查询参数而不是基本身份验证传递客户端id和客户端机密)?如果是这样,你需要先打电话。太棒了。加上这一点后,它起了作用。谢谢,请在发布代码之前,先输入您的代码。。。这对你来说是一种痛苦,我没有修改代码。它现在正在正确显示。您是否也这么看?OAuth服务器是否已“损坏”(即,它是否希望通过查询参数而不是基本身份验证传递客户端id和客户端机密)?如果是这样,你需要先打电话。太棒了。加上这一点后,它起了作用。谢谢蒂姆