通过Golang 401错误进行Soundcloud API身份验证

我正在尝试连接到Soundcloud API并在Golang中获取令牌,但是却收到401错误消息,提示“错误”:“ invalid_client”。

我已经验证了客户ID和机密。

我的重定向URI存在并且是:

http://localhost:8080/platform/soundcloudCallback.html

我的代码如下:

func main() {

v := url.Values{}

v.Set("scope", "non-expiring")

v.Set("client_id", auth.ClientID)

v.Set("response_type", "code")

v.Set("redirect_uri", auth.RedirectURI)

c.AuthURL = AuthEndpoint + "?" + v.Encode()

c.Values = v.Encode()

res := c.Request("POST", url.Values{})

}

func (c *Client) Request(method string, params url.Values) []byte {

params.Set("client_id", "*************")

reqUrl := "https://api.soundcloud.com/oauth2/token"

req, _ := http.NewRequest(method, reqUrl, strings.NewReader(c.Values))

req.Header.Add("Accept", "application/json")

resp, _ := c.client.Do(req)

defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

return body

}

我在NewRequest中的身体是否不正确,还是其他原因导致了此问题?还不清楚localhost是如何与API一起工作的。

解决方案是确保您具有以下所有条件:

v.Set("scope", "non-expiring")

v.Set("client_id", auth.ClientID)

v.Set("client_secret", "f5e416ddf95aed8d077fccccc0a07821")

v.Set("response_type", "code")

v.Set("redirect_uri", auth.RedirectURI)

v.Set("grant_type", "authorization_code")

对于那些对此感到困惑的人,我在blog.rileedesign.com上发表了一篇博客文章,详细介绍了所有内容。

回答:

我不知道您是否通过了正确的身份验证过程。首先,您需要在SoundCloud上设置一个应用程序-之所以这样做,是因为您有一个客户端密码和一个客户端ID。

然后,您打开SoundCloud登录页面,输入用户名和密码,然后(如果您成功登录)使用授权码将您重定向到Redirect

URI。该代码非常重要,因为使用该代码您可以获得访问令牌。

如果你把

v.Set("grant_type", "authorization_code")

您还需要使用以下命令设置授权代码:

v.Set("code", AUTHORIZATION_CODE)

之后,您会从SoundCloud获得带有访问令牌,刷新令牌等的响应。

编辑:

因此,例如您的重定向URI如下所示

http://redirect.uri

然后,当用户成功通过身份验证后,您将被重定向到包含身份验证代码的URI。它看起来像这样:

http://redirect.uri/?code=AUTHENTICATION_CODE

然后您向发出POST请求

https://api.soundcloud.com/oauth2/token

包括您的验证码,客户端ID和客户端密码。响应将包括访问令牌,刷新令牌等。

以上是 通过Golang 401错误进行Soundcloud API身份验证 的全部内容, 来源链接: utcz.com/qa/407781.html

回到顶部