Weble / ZohoBooksApi

40 stars 39 forks source link

Use refresh token to get authtoken if expired #12

Closed iamraju closed 5 years ago

iamraju commented 5 years ago

Is there a way to recreate authtoken if it expires ? Maybe like we pass refresh token fourth parameter of constructor then handle if the given token has expired !

By the way, I did not find a way to check if the given auth token is expired !

PS: This is not an issue but a question :smile:

Skullbock commented 5 years ago

Hi @iamraju you can set a "refresh token" using $zohoBooks->setRefreshToken($token);

This will instruct the api client to use that to refresh the access token if the apis reply that the access token is expired.

The access token expiration date is contained in the access_token generation response, but we currently don't track it. We just try to use it, and if the api answer that it is expired, then we regenerate it with the refresh token. If you would like to supply a better version, i'll gladly accept a PR :)

Hope it helps!

iamraju commented 5 years ago

Hi @Skullbock Thank you for the quicker reply ! Looks like you are on the desk :smile:

It is good to know that there is a method setRefreshToken() and I will to use it !

But the library is throwing me invalid token whereas normal PHP's curl functions and curl command from the terminal works fine with the same auth/access token strangely !

Do you have any insight why the library is throwing invalid access token ?

Thanks in advance !

PS: I will try to have tracking the time but if the API itself returns token expires, that should be enough. The ultimate goal is to know whether the error is due to expiry or something else :smile:

Skullbock commented 5 years ago

that's strange... and it would be a difficult issue to track for me since it's related to authentication. Can you paste at least the reply the exception receive from the apis?

Skullbock commented 5 years ago

Maybe it's related to this, and you want to take a look at it?

https://github.com/Weble/ZohoBooksApi/issues/10

iamraju commented 5 years ago

Maybe it's related to this, and you want to take a look at it?

10

Looks like this but it did not help by changing the header key name to Zoho-oauthtoken either ! The error message is simply Response from Zoho is not success. Message: Invalid value passed for authtoken.

iamraju commented 5 years ago

Simple PHP is working though:

$url = "https://books.zoho.com/api/v3/invoices/?organization_id=xxx";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json",
            "Authorization: Zoho-oauthtoken 1000.xxx.xxxx"
        ));
        $invoices = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $httpCode == 200 ? $invoices : false;
Skullbock commented 5 years ago

Then it's definitely #10 because of the new auth method.

On Mon, Sep 16, 2019 at 11:32 AM Raju Gautam notifications@github.com wrote:

Simple PHP is working though:

$url = "https://books.zoho.com/api/v3/invoices/?organization_id=xxx"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Authorization: Zoho-oauthtoken 1000.xxx.xxxx" )); $invoices = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $httpCode == 200 ? $invoices : false;

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Weble/ZohoBooksApi/issues/12?email_source=notifications&email_token=AAINRUZAMZYSWKTHVW2IPTLQJ5HCXA5CNFSM4IW7D6T2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6YSVYQ#issuecomment-531704546, or mute the thread https://github.com/notifications/unsubscribe-auth/AAINRU6QWRNNW7PAYDPZ5ILQJ5HCXANCNFSM4IW7D6TQ .

-- [image: Logo Weble] Daniele Rosario

CTO Weble Srl Via Enrico Fermi, 265 36100 Vicenza ufficio: +39 0444 1454934 <003904441454934> mobile: +39 3283017134 <00393283017134> weble.it https://www.weble.it/ https://www.facebook.com/pages/Weble/110077192371908?fref=ts https://twitter.com/Webleit?lang=it https://www.linkedin.com/company/2470613?trk=tyah&trkInfo=clickedVertical%3Acompany%2Cidx%3A2-2-7%2CtarId%3A1432717146425%2Ctas%3Aweble https://www.instagram.com/weble.it/

iamraju commented 5 years ago

Since changing the key name did not work and I do not have much time to spend on this so I will continue with my core PHP solution for now, I will look back on this and get back to you if I have anything or I can add PR !

Thanks for your time :smile:

bluec commented 5 years ago

Probably some confusion here since the Books API docs refer only to OAuth and this module does not yet implement OAuth but still uses the old and deprecated authtoken mechanism.

FYI, since they removed this from the Books API docs, here is the URL to get yourself an authtoken to use with this module:

https://accounts.zoho.com/apiauthtoken/create?SCOPE=ZohoBooks/booksapi

iamraju commented 5 years ago

Probably some confusion here since the Books API docs refer only to OAuth and this module does not yet implement OAuth but still uses the old and deprecated authtoken mechanism.

FYI, since they removed this from the Books API docs, here is the URL to get yourself an authtoken to use with this module:

https://accounts.zoho.com/apiauthtoken/create?SCOPE=ZohoBooks/booksapi

Agree ! Later I came to know that this package does not support oauth and hence it did not work even the header key changed either ! I am already done for my task with PHP's curl functions and happy :smile:

Skullbock commented 5 years ago

Yeah, we don't use this package anymore, but i'll try to create a new auth package so all our zoho libraries work with the same auth methods