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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源