schmorrison / Zoho

Golang API for Zoho services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Self-client] CRM InsertRecord return empty InsertRecordResponse without err

kiuKisas opened this issue · comments

I use your lib to insert new Lead and Contact when receiving a notification, to do that I use the self-client way to authorize and the InsertRecord from the crm package.
When I first start the service, everything is going well a new lead/contact is created, InsertRecord return the data, and everyone is happy.
But curiously after some times, the call doesn't work anymore, but InsertRecord return no error and an empty InsertRecordResponse equal to { [] }.
However, when I restart the app, the api call works well again so I guess it have something to do with authorization

Hi there, glad you are finding this project useful.

I believe you are right, from what I remember, currently it is the users responsibility to check the tokens expiry and issue a token refresh request before the expiry occurs.

import (
    "log"
    "github.com/schmorrison/Zoho"
)

func main() {
    z := zoho.New()

    // to start oAuth2 flow
    scopes := []zoho.ScopeString{
        zoho.BuildScope(zoho.Crm, zoho.ModulesScope, zoho.AllMethod, zoho.NoOp),
    }

    if err := z.GenerateTokenRequest("yourClientID", "yourClientSecret", "authorizationCode", "redirectURL"); err != nil {
        log.Fatal(err)
    }

    //// DO SOME THINGS
   err = zoho.RefreshTokenRequest()
   if err != nil {
       log.Fatal(err)
   }

// DO SOME MORE
}

If you want to write custom logic for managing your tokens you can write a struct that satisfies the interface TokenLoaderSaver and you could setup a timer to check the expiry every x minutes and request a token refresh as you decide.

I should let you know that there is a pull request in the works that should automatically refresh the token by checking before each HTTP request. There is no guarantee when this PR will be merged tho.

Thank's for your answer, I did a "dirty" trick to initialise Zoho again if it fail, I'll change that asap. Are you talking about this PR ?

Yeah I am, but looking over the PR now, I cant actually locate the lines I was thinking of.

Closed?

Closed 👍