dongyanfeng0546 2016-07-01 07:35
浏览 12
已采纳

如何直接将Podio AuthToken传递给podio-go客户端

I'm a Ruby on Rails developer and am now venturing into Go for doing some podio.com API calls. Everything went smoothly so far but I bumped into a small and stupid problem for which I find no answer on the mighty stackoverflow (probably because I don't know what to search for).

A bit of context: Our rails app is already handling all the podio oAuth server-flow we have all the necessary client information (auth_token, refresh_token, ...) and do not need to request that information again.

Instead of doing

 podio.AuthWithAuthCode(clientId, clientSecret, authCode, redirectUri)

I wish to directly create a client object like so

// json data coming from rails
json_auth_token := "{\"access_token\":\"...\",\"refresh_token\":\"...\",\"token_type\":\"\",\"expires_in\":28800,\"transfer_token\":\"\"}"

// creating a AuthToken object
authToken := podio.AuthToken{}
json.Unmarshal([]byte(json_auth_token), &authToken)

// so far so good, but when doing
client := podio.NewClient(authToken)

I get the error

cannot use authToken (type podio.AuthToken) as type *podio.AuthToken in argument to podio.NewClient

Can somebody help me with this? It is not necessarily Podio related. More go related

  • 写回答

1条回答 默认 最新

  • dongshi1188 2016-07-01 08:06
    关注

    Currently the type of the variable authToken is podio.AuthToken, i.e. not a pointer, but simply a struct. The function podio.NewClient expects an argument of type *podio.AuthToken (pointer to podio.AuthToken), which is why you're getting a compiler error. The simplest way to fix this would be to simply deference authToken like so:

    client := podio.NewClient(&authToken)
    

    Alternatively you can change the type of authToken to *podio.AuthToken:

    authToken := &podio.AuthToken{}
    json.Unmarshal([]byte(json_auth_token), authToken)
    client := podio.NewClient(authToken)
    

    Slightly unrelated (and maybe you omitted this for brevity), but you probably want to check the error returned from json.Unmarshal:

    err := json.Unmarshal([]byte(json_auth_token), authToken)
    if err != nil {
      // handle error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大