Youtube API无效的授权和缺少必需的参数:grant_type

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

Youtube API Invalid Grant and Required parameter is missing: grant_type

问题

我在使用Youtube API的OAuth进行身份验证时遇到了问题。它给了我以下错误:

  • "error" : "invalid_grant"(对于CURL)
  • "error" : "invalid_request",
    "error_description" : "Required parameter is missing: grant_type"(对于GoLang)

这是我的CURL代码:

	-F 'code=CODE_FROM_MY_USER' \
	-F 'client_id=MY_CLIENT_ID' \
	-F 'client_secret=MY_CLIENT_SECRET' \
	-F 'redirect_uri=http://localhost:8080/platform/youtubeCallback.html' \
	-F 'grant_type=authorization_code'

这是我的Go代码,已经验证可以用于Soundcloud API。

func Auth(code string) error {
	v := url.Values{}
	v.Set("code", code)
	v.Set("client_id", "MY_CLIENT_ID")
	v.Set("client_secret", "MY_CLIENT_SECRET")
	v.Set("redirect_uri", "http://localhost:8080/platform/youtubeCallback.html")
	v.Set("grant_type", "authorization_code")

	params := strings.NewReader(v.Encode())

	req, err := http.NewRequest("POST", "https://accounts.google.com/o/oauth2/token", params)
	if err != nil {
		return err
	}
	req.Header.Add("Accept", "application/json")

	resp, err := c.Client.Do(req)
	if err != nil || resp.StatusCode != 200 {
		return err
	}

	json.NewDecoder(resp.Body).Decode(&c.Token)

	return nil
}

以上是翻译好的内容,请确认是否正确。

英文:

I'm having trouble authenticating with the Youtube API via OAuth. It gives me this error:

  • "error" : "invalid_grant" for CURL
  • "error" : "invalid_request",
    "error_description" : "Required parameter is missing: grant_type" for GoLang

This is my CURL code:

	-F 'code=CODE_FROM_MY_USER' \
	-F 'client_id=MY_CLIENT_ID' \
	-F 'client_secret=MY_CLIENT_SECRET' \
	-F 'redirect_uri=http://localhost:8080/platform/youtubeCallback.html' \
	-F 'grant_type=authorization_code'

This is my Go code and has been verified to work for the Soundcloud API.

func Auth(code string) err {
	v := url.Values{}
	v.Set("code", code)
	v.Set("client_id", "MY_CLIENT_ID")
	v.Set("client_secret", "MY_CLIENT_SECRET")
	v.Set("redirect_uri", "http://localhost:8080/platform/youtubeCallback.html")
	v.Set("grant_type", "authorization_code")

	params := strings.NewReader(v.Encode())

	req, err := http.NewRequest("POST", "https://accounts.google.com/o/oauth2/token", params)
	if err != nil {
		return err
	}
	req.Header.Add("Accept", "application/json")

	resp, err := c.Client.Do(req)
	if err != nil || resp.StatusCode != 200 {
		return err
	}

	json.NewDecoder(resp.Body).Decode(&c.Token)

	return nil
}

答案1

得分: 2

原文:It turns out you need to define the content type:

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

翻译结果:原来你需要定义内容类型:

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

英文:

It turns out you need to define the content type:

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

答案2

得分: 1

以下是翻译好的内容:

如果你创建一个 map[string][]string 并将其传递给 url.Values,也可以正常工作,如下所示:

namesToSend := map[string][]string{
  "firstname": {"Bob"},
}
values := url.Values{}
values = namesToSend

然后在 .NewRequest() 中像传递参数一样传递它。

req, err := http.NewRequest("POST", "https://accounts.google.com/o/oauth2/token", values)
if err != nil {
    return err
}
英文:

It also works if you create a map[string][]string and pass it to url.Values like:

namesToSend := map[string][]string{
  "firstname":{Bob}
}
values := url.Values{}
values = namesToSend

Then in the .NewRequest() you pass it like you passed the params.

req, err := http.NewRequest("POST", "https://accounts.google.com/o/oauth2/token", values)
if err != nil {
    return err
}

huangapple
  • 本文由 发表于 2014年8月23日 00:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/25451805.html
匿名

发表评论

匿名网友

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

确定