dtbl1231 2014-07-28 16:52
浏览 50
已采纳

使用huandu / facebook Golang FB API时出现DecodeField错误

I was trying to extract FB user info, feed, etc. with Result.DecodeField after a batch request, by using huandu/facebook Golang FB api. Below is the simplified version with a single request in a batch.

I was blocked by this for a whole day. Deep appreciate if you can help check this problem!

code:

type User struct {
    Id    string `json:"id" facebook:"id"`
    Name  string `json:"name" facebook:"name"`
}

func batchRequests() {

    paramsId := fb.Params{
        "method":       fb.GET,
        "relative_url": "me",
    }

    results, errBatch := pSession.BatchApi(paramsId)
    if errBatch != nil {
        fmt.Println("Batch api error:", errBatch.Error())
        return
    }

    fmt.Println("Results[0]:", results[0])

    var user User
    if err := results[0].DecodeField("body", &user); err != nil {
        fmt.Println("Decode user err:", err.Error())
    } else {
        fmt.Println("user", res0)
    }

    return 
}

Debug msg:

Results[0]: map[code:200 headers:[map[name:Last-Modified value:2014-05-26T02:20:09+0000] map[name:Facebook-API-Version value:v2.0] map[name:ETag value:"53abd9d236bfbd61662d1139e66983f8d0220d1e"] map[name:Content-Type value:text/javascript; charset=UTF-8] map[name:Pragma value:no-cache] map[name:Access-Control-Allow-Origin value:*] map[name:Cache-Control value:private, no-cache, no-store, must-revalidate] map[name:Expires value:Sat, 01 Jan 2000 00:00:00 GMT]] body:{"id":"10152276269XXXXXX","first_name":"XXXX","gender":"male","last_name":"XXXX","link":"https://www.facebook.com/app_scoped_user_id/1015227626XXXXXX/","locale":"zh_TW","name":"XXXXXX","timezone":8,"updated_time":"2014-05-26T02:20:09+0000","verified":true}]

Decode user err: field 'body' is not a json object in result.

  • 写回答

2条回答 默认 最新

  • dqmgjp5930 2014-12-06 17:13
    关注

    Library document was misleading. Facebook always returns a "body" field as string in batch api response. So "body" cannot be decoded to user struct directly.

    I just add a new BatchResult struct in library to store a parsed batch api response. Here is a sample to use this new struct to decode User.

    // error handling code is omitted.
    batchResult, _ := results[0].Batch()
    
    var user User
    batchResult.Result.Decode(&user)
    fmt.Println("decode user", user)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持