英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论