Soundcloud API Auth通过Golang 401错误 [英] Soundcloud API Auth via Golang 401 Error

查看:207
本文介绍了Soundcloud API Auth通过Golang 401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到Soundcloud API并在Golang中获得一个令牌,但是我得到一个401 errr说error:invalid_client。



我已验证客户ID和密码。



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

  http://我的代码如下:

$ b $ local $
$ b

  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)请求(方法字符串,参数url.Values)[]字节{
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)

返回正文
}

在NewRequest中我的正文是不正确还是其他内容导致问题?目前还不清楚localhost如何与API协同工作。



解决方案确保您拥有以下所有内容:



<$ (范围,非过期)
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登录页面,输入您的用户名和密码,然后(如果您已成功登录),您将被重定向到带有授权码的重定向URI。该代码非常重要,因为通过该代码,您可以获取访问令牌。



如果您输入

 v.Set(grant_type,authorization_code)

您还需要设置授权码:

  v.Set(code,AUTHORIZATION_CODE)

之后,您将通过SoundCloud获得访问令牌,刷新令牌等的响应。



编辑:

因此,例如你的重定向URI看起来像这样

  http://redirect.uri 

然后,当用户认证成功时,您将被重定向到包含认证码的URI。它看起来像这样:

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


$ b然后你发一个POST请求到

$ $ p $ $ $ $ $ c> https://api.soundcloud.com/oauth2/token

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


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.

解决方案

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.

这篇关于Soundcloud API Auth通过Golang 401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆