dougai6464 2016-04-13 05:11
浏览 26
已采纳

FB交换令牌API抛出错误

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条回答 默认 最新

  • duanpu1064 2016-04-13 05:57
    关注

    The error message says that your username for Basic Auth must be 1543358959292867, yet the client_id you provide is actually 1543358959292867 <- 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("|%s|", 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效