dongpa2000 2014-08-08 16:55
浏览 358
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • douzhan1031 2014-08-08 19:58
    关注

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

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题