Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Go with formdata和身份验证中发出POST请求_Go - Fatal编程技术网

在Go with formdata和身份验证中发出POST请求

在Go with formdata和身份验证中发出POST请求,go,Go,我目前正在尝试使用示例curl命令curl-u{client\u id}:{client\u secret}-d grant\u type=client\u凭证与OAuth api接口https://us.battle.net/oauth/token。我当前的go文件是: package main import ( "bytes" "fmt" "mime/multipart" "net/http" ) func checkErr(err error) bool

我目前正在尝试使用示例curl命令
curl-u{client\u id}:{client\u secret}-d grant\u type=client\u凭证与OAuth api接口https://us.battle.net/oauth/token
。我当前的go文件是:

package main

import (
    "bytes"
    "fmt"
    "mime/multipart"
    "net/http"
)

func checkErr(err error) bool {
    if err != nil {
        panic(err)
    }
    return true
}

func authcode(id string, secret string, cli http.Client) string {
    //un(trace("authcode"))
    var form bytes.Buffer
    w := multipart.NewWriter(&form)
    _, err := w.CreateFormField("grant_type=client_credentials")
    checkErr(err)
    req, err := http.NewRequest("POST", "https://us.battle.net/oauth/token", &form)
    checkErr(err)
    req.SetBasicAuth(id, secret)
    resp, err := cli.Do(req)
    checkErr(err)
    defer resp.Body.Close()
    json := make([]byte, 1024)
    _, err = resp.Body.Read(json)
    checkErr(err)
    return string(json)
}

func main() {
    //un(trace("main"))
    const apiID string = "user"
    const apiSecret string = "password"
    apiClient := &http.Client{}
    auth := authcode(apiID, apiSecret, *apiClient)
    fmt.Printf("%s", auth)
}
当我运行这个程序时,我得到的响应是
{“error”:“invalid_request”,“error_description”:“Missing grant type”}

作为参考,api流状态为:
“若要请求访问令牌,应用程序必须使用以下多部分表单数据向令牌URI发出POST请求:grant_type=client_凭据

应用程序必须使用客户端id作为用户,客户端密码作为密码来传递基本HTTP身份验证凭据。“

预期的响应是一个json字符串,其中包含访问令牌、令牌类型、以秒为单位的到期时间以及curl手册中所述令牌可用的函数范围,我们有:

       -d, --data <data>
          (HTTP)  Sends  the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and
          presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.   Compare  to
          -F, --form.
因此,根据您的
curl
mime/multipart
可能不是您想要的,您应该使用,我们的手册中有:

The Content-Type header is set to application/x-www-form-urlencoded. To set other headers, use NewRequest and Client.Do.

curl命令指出文档在提到多部分请求时是错误的。尝试作为请求主体
strings.NewReader(“grant\u type=client\u credentials”)
(但对于任何更复杂的使用,它可以正确处理转义)。在任何情况下,您调用CreateFormField都是错误的:参数只是字段名,而不是名称和值。@peter我试过了,但仍然得到了无效的请求错误您可能还必须设置内容类型标题:
req.header.set(“Content-Type”,“application/x-www-form-urlencoded”)
。仍然没有什么不幸的事,尽管如此,我把代码打错了,所以id被关闭了。非常感谢你!PostForm是否允许我继续使用“SetBasicAuth(id,secret)”部分,或者我需要做其他事情?我尝试了另一个响应并添加了“strings.NewReader(“grant\u type=client\u credentials”)”作为主体,但它仍然给了我无效的请求
The Content-Type header is set to application/x-www-form-urlencoded. To set other headers, use NewRequest and Client.Do.