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



  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 C#点击按钮的时候的循环次数就是最后一次,如何是循环第几次的值?
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写