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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?