PayPal OAuth:即使指定了主机,仍然出现400错误。

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

PayPal OAuth: 400 Error even after specifying Host

问题

我正在使用golang进行这个项目,并且我已经整夜都在尝试让它工作。我最近遇到的问题是,尽管我已经添加了许多头部信息到我的请求中,但仍然收到400错误。

29     req.SetBasicAuth(app.id, app.secret)
30     req.Header.Set("Host", "www.sandbox.paypal.com")
31     req.Header.Set("Accept", "application/json")
32     req.Header.Set("Accept-Language", "en_US")
33     req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
34     req.Header.Set("Connection", "close")

我已经设置了身份验证,并添加了我能想到的所有头部信息。我已经仔细阅读了文档,并尝试了一些方法来解决问题。这是我在POST请求的正文中传递的信息:

{"grant_type": "client_credentials"}

42     req, err := http.NewRequest(
43         "POST", 
44         app.Endpoint + oauthEndpoint,
45         strings.NewReader(`{"grant_type": "client_credentials"}`),
46     )

如果你需要更多的代码,请告诉我。但我不确定它是否会有太大的帮助。我还希望将POST请求尽可能地通用化。

编辑:

尽管我在有效载荷中手动指定了grant_type,但似乎失败了,因为它无法识别grant_type

{"error":"invalid_request","error_description":"grant_type is a required parameter"}

英文:

I'm doing this one in golang, and I've been up just about the entire night trying to get it to work. My latest frustration is that I'm getting a 400 error despite the numerous headers I've dropped into my request.

 29     req.SetBasicAuth(app.id, app.secret)
 30     req.Header.Set("Host", "www.sandbox.paypal.com")
 31     req.Header.Set("Accept", "application/json")
 32     req.Header.Set("Accept-Language", "en_US")
 33     req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
 34     req.Header.Set("Connection", "close")

I have auth set up, and every header I could think to add. I've pored over the documentation and have tried the poke it to see if it works approach as well. Heres the information I've been passing in the body with the post request

`{"grant_type": "client_credentials"}`

 42     req, err := http.NewRequest(
 43         "POST", 
 44         app.Endpoint + oauthEndpoint,
 45         strings.NewReader(`{"grant_type": "client_credentials"}`),
 46     )

If you need some more of the code just tell me. But I'm not super sure it'll be a whole lot of good. I wanted to make the post as general as possible as well.

EDIT:

So it seems that despite the fact I am specifying the grant_type in the payload manually it is failing because grant_type is not being recognized

{"error":"invalid_request","error_description":"grant_type is a required parameter"}

答案1

得分: 2

问题出在我的读取器对象上,它应该是

strings.NewReader("grant_type=client_identifier")
英文:

Problem was my reader object, it should have been

strings.NewReader("grant_type=client_identifier")

答案2

得分: -1

Header.Set()方法并不会添加一个新的头部信息。你应该使用Header.Add()方法。请参考上面的文档链接。

英文:

http://golang.org/pkg/net/http/#Header.Set

Header.Set() doesn't add a new header. You want Header.Add(). See the documentation link above.

huangapple
  • 本文由 发表于 2013年8月25日 21:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/18429427.html
匿名

发表评论

匿名网友

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

确定