FB交换令牌API出现错误。

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

FB exchange token api throwing error

问题

我正在对开发服务器上的GET请求进行测试,以交换代码获取访问令牌。

Oauth表单URL:

https://www.facebook.com/dialog/oauth?
    client_id=1543358959292867%0A&
    redirect_uri=http%3A%2F%2Flocalhost%3A8080%2FFBLoginCallback&
    response_type=code&
    scope=email+user_birthday+user_about_me

带有代码的回调:

http://localhost:8080/FBLoginCallback?code=AQAsGssRxA9nDZ2YQ82Dgdw_NB_MsikZxVanO75QG0hKlH9F49v7CEiu5ssBHLmvF_hc4081Q5KcJq9cgC9pyIacz-ekpL6WKv5x0E12-HRzvR7bSImWJMktfTIfbabIDDy3BsOue2GhGos7qlIbU-XJGrzWMehbEhAb4p-2rpd4bVnIqKvaErU2Ma8onUMM8HICyf7IaArOlZs5VYqQDO0IjxsTot5DdU0j8IyBdmtksdspPK-YMhFcJTXabAxO2tWnvOQhAMCH00rlIByCY-xXMIgiLfrnF_YRrs9x5lU2d8ZF2DZrHgXLfnxH1Hh-bGQ#_=_

我得到以下响应。

oauth2: 无法获取令牌:400 Bad Request
响应:{"error":{"message":"您的HTTP基本授权的用户名必须为1543358959292867","type":"OAuthException","code":101,"fbtrace_id":"HTJcNHH6OmY"}}

问题是这个错误发生在我的两台笔记本电脑中的一台上。它们都运行完全相同的代码。

这是我用于交换的golang函数。

tok, err := fbConfig.Exchange(oauth2.NoContext, r.FormValue("code"))
英文:

I am testing a GET request on a development server to exchange a code for an access token.

Oauth form URL:

https://www.facebook.com/dialog/oauth?
    client_id=1543358959292867%0A&
    redirect_uri=http%3A%2F%2Flocalhost%3A8080%2FFBLoginCallback&
    response_type=code&
    scope=email+user_birthday+user_about_me

Callback w/code.

http://localhost:8080/FBLoginCallback?code=AQAsGssRxA9nDZ2YQ82Dgdw_NB_MsikZxVanO75QG0hKlH9F49v7CEiu5ssBHLmvF_hc4081Q5KcJq9cgC9pyIacz-ekpL6WKv5x0E12-HRzvR7bSImWJMktfTIfbabIDDy3BsOue2GhGos7qlIbU-XJGrzWMehbEhAb4p-2rpd4bVnIqKvaErU2Ma8onUMM8HICyf7IaArOlZs5VYqQDO0IjxsTot5DdU0j8IyBdmtksdspPK-YMhFcJTXabAxO2tWnvOQhAMCH00rlIByCY-xXMIgiLfrnF_YRrs9x5lU2d8ZF2DZrHgXLfnxH1Hh-bGQ#_=_

I get the following response.

oauth2: cannot fetch token: 400 Bad Request
Response: {"error":{"message":"The username for your HTTP Basic Authorization must be 1543358959292867","type":"OAuthException","code":101,"fbtrace_id":"HTJcNHH6OmY"}}

The issue is that this error occurs on one of my two laptops. They both run the exact same code.

This is the golang function I am using for the exchange.

tok, err := fbConfig.Exchange(oauth2.NoContext, r.FormValue("code"))

答案1

得分: 2

错误消息表示,您的基本身份验证用户名必须是1543358959292867,但您提供的client_id实际上是1543358959292867\n,末尾附加了换行符(在查询参数中编码为%0A)。

我认为问题不一定与不同的机器有关,而是与使用这些不同机器的输入有关。

一个简单的方法是记录client_id,这样您可以在它正常工作和不正常工作时查看它的值。当您记录它时,您可能希望用已知字符包装它,以便您可以看到换行符。例如,

fmt.Printf("|%s|", client_id)

如果client_id包含换行符,则日志将显示类似于

|1543358959292867
|

而不是

|1543358959292867|

您还可以使用strings.TrimSpace来删除任何前缀或后缀的空格,包括换行符。

client_id = strings.TrimSpace(client_id)
英文:

The error message says that your username for Basic Auth must be 1543358959292867, yet the client_id you provide is actually 1543358959292867\n <- newline tacked on the end (encoded as %0A in your query params).

I don't think the issue is necessarily tied to being different machines, but the input that is being used from those different machines.

A simple thing to do would be log the client_id so you can see what it is when it works and what it is when id doesn't. When you log it you may want to wrap it with a known character so you can see newlines. For example,

fmt.Printf(&quot;|%s|&quot;, client_id)

If client_id contains a newlines then that will log something like

|1543358959292867
|

instead of

|1543358959292867|

You could also use strings.TrimSpace to remove any prefix or suffix whitespace including newlines.

client_id = strings.TrimSpace(client_id)

huangapple
  • 本文由 发表于 2016年4月13日 13:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/36589109.html
匿名

发表评论

匿名网友

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

确定