dsf8897 2015-10-06 20:28
浏览 353

解析Github的access_token响应

Just messing around with Github API and oauth. I have got to the point where I receive the access_token back from GH.

I have so far:

url := "https://github.com/login/oauth/access_token"

params := map[string]string{"client_id": client_id, "client_secret": client_secret, "code": code}
data, _ := json.Marshal(params)
resp, _ := http.Post(url, "application/json", bytes.NewBuffer(data))

defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

but I would now like to access the response parts. According to the GH docs, they are in the form access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&scope=user%2Cgist&token_type=bearer

Do I need to parse the string or is there a "better" way?

  • 写回答

1条回答 默认 最新

  • dpuwov1487 2015-10-06 20:52
    关注

    This is a URL query string. You can use the url package to parse it and get a url.Values (which is just a map) out.

    resp := "access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&scope=user%2Cgist&token_type=bearer"
    values, err := url.ParseQuery(resp)
    if err != nil {
        panic(err)
    }
    
    fmt.Println("access_token:", values["access_token"])
    fmt.Println("token_type:", values["token_type"])
    

    Play link

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题