Soundcloud API通过Golang进行身份验证时出现401错误。

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

Soundcloud API Auth via Golang 401 Error

问题

我正在尝试连接到Soundcloud API并在Golang中获取令牌,但是我收到一个401错误,错误信息为"error":"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中的body是否有误,还是其他原因导致了问题?关于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上写了一篇详细的博文。

英文:

I'm attempting to connect to the Soundcloud API and obtain a token in Golang, but I get a 401 errr saying, "error":"invalid_client".

I've verified client ID and secret.

My redirect URI exists and is:

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

My code is as follows:

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
}

Is my body in the NewRequest incorrect or is something else causing the issue? It's very unclear how localhost works with the API.

The solution is ensuring you have all of the following:

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")

For anyone stuck on this, I made a blog article at blog.rileedesign.com detailing everything.

答案1

得分: 1

我认为你不仅需要设置客户端ID,还需要设置客户端密钥。

我也想要一个永不过期的访问令牌,但不知何故这并不起作用。所以每次访问令牌过期时,我都会刷新我的访问令牌。

英文:

I think you also need to set the client secret, not only your client id.

I also wanted a non-expiring access token, but somehow this is not working. So I'm refreshing my access token everytime it is expired.

答案2

得分: 1

我不知道你是否正确完成了身份验证过程。首先,你需要在SoundCloud上设置一个应用程序-你已经做到了,因为你有一个客户端密钥和客户端ID。

然后你打开SoundCloud登录页面,输入你的用户名和密码,然后(如果你成功登录)你会被重定向到带有授权代码的重定向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和客户端密钥。响应将包含访问令牌、刷新令牌等。

英文:

I don't know if you got the authentication process right. At first, you need to set up an app on SoundCloud - you have done that, because you have a client secret and a client id.

Then you open the SoundCloud login page, enter your username and password, and then (if you're logged in successfully) you're redirected to the Redirect URI with the authorization code. That code is very important, because with that code you can obtain the access token.

If you put in

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

you also need to set the authorization code with:

v.Set("code", AUTHORIZATION_CODE)

After that you'll get a response from SoundCloud with the access token, refresh token and so on..

EDIT:

So, for example your Redirect URI looks like this

http://redirect.uri

then, when the user authenticated successfully, you'll get redirected to that URI including the authentication code. It will look like this:

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

Then you make a POST request to

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

including your authentication code, client id and client secret. The response will include the access token, refresh token and so on.

答案3

得分: 1

也许你想要检查一下这个。

Soundcloud Oauth2 的 Go 实现

https://github.com/vitorsvvv/go-soundcloud-oauth

英文:

Maybe you want to check this.

Go implementation of Soundcloud Oauth2

https://github.com/vitorsvvv/go-soundcloud-oauth

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

发表评论

匿名网友

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

确定