Soundcloud API Auth via Golang 401 Error

北城余情 提交于 2019-12-02 12:51:32

问题


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:


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.




回答2:


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.




回答3:


Maybe you want to check this.

Go implementation of Soundcloud Oauth2

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



来源:https://stackoverflow.com/questions/25208459/soundcloud-api-auth-via-golang-401-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!