【发布时间】:2021-07-21 00:23:18
【问题描述】:
我正在尝试使用 golang 对 Azure 服务管理/图形 API 进行身份验证。 使用纯 REST API。 不管我做什么,我总是以错误告终:
{"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.
由于我没有使用 SDK,因此那里的示例有限。任何帮助将不胜感激。
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
func main() {
authendpoint := "https://login.microsoftonline.com/8xxxxx7-6372-4bcb-xxx-xxxxxx/oauth2/token"
jsonData := []byte(`{
"resource": "https://graph.microsoft.com",
"client_id": "xxxxxxxx-7549-4ea2-b00d-xxxxxxxxxxx",
"client_secret": "Q.xxxxxxxxxxxxxx-6_CgA4yOi_8sS-",
"grant_type": "client_credentials",
}`)
request, err := http.NewRequest("POST", authendpoint, bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
var res map[string]interface{}
json.NewDecoder(resp.Body).Decode(&res)
log.Println(string(body))
}
【问题讨论】:
-
如果我没记错的话,它期望
Content-Type是application/x-www-form-urlencoded,而不是application/json -
根据文档,您的实现略有错误。 docs.microsoft.com/en-us/graph/auth/…
-
JSON 格式无效。在 JSON 数据中删除
"client_credentials"之后的,。 -
@cod3rboy 之前尝试过,现在再次尝试。它没有帮助。
标签: azure go oauth-2.0 azure-rest-api