2

I see that the workflow is to start authrorizer, giving it file loader. So, we have a sequence of callbacks, onAuthrorized => start loading file => doc.getModel() on file load. Here they say how you get the model. But, I also see that gapi.drive.realtime.load(fileId, onFileLoaded, initializeModel, handleErrors) can elso end up with TOKEN_REFRESH_REQUIRED and it seems that TOKEN_REFRESH_REQUIRED can fire after the document is loaded, after some time of user inactivity, which seems to be related with token expiration. How should re-authorization to go? Should I tell the client that the current model that he is connected to is invalid? Please note that my app starts on file load. So, if I go the whole stack re-authorization, which calls another file load, which calls another document loaded will re-start my application. Is it supposed way to go? To put in other words, is there a way to refresh the token without loosing existing connection?

Where is the token stored actually? I do not see that I receive it on authorized. It is not passed to the realtime.load. How does realtime.load knows about the token? How can I speed up the token expiration for debug?

Little Alien
  • 1
  • 8
  • 22

1 Answers1

0

I am still not sure that this is a right answer but this is what I have got looking at code here, which says that we should provide empty callback to re-authorize

/**
 * Reauthorize the client with no callback (used for authorization failure).
 * @param onAuthComplete {Function} to call once authorization has completed.
 */
rtclient.Authorizer.prototype.authorize = function(onAuthComplete) {
    function authorize() {
        gapi.auth.authorize({client_id: rtclient.id, scope: ['install', 'file'],}, handleAuthResult)
    }

    function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
            hideAuthorizationButton() && onAuthComplete()
        } else with (authorizationButton) {
          display = 'block' ; 
          onclick = authorize;
        }
    }

You call it first use it in a function to load your document

(rtclient.authorizer ? rtclient.authorizer = identity : rtclient.authorize) (proceedToLoadingTheFile)

But later, on timeout we have code

function handleErrors(e) { with(gapi.drive.realtime.ErrorType) {switch(e.type) {
    case TOKEN_REFRESH_REQUIRED: rtclient.authorizer.authorize() ; break
    case CLIENT_ERROR: ...

Note no arguemnts in the latter. Authorizer won't reload the document. I think that this explains the logic asked. However, it does not answer about the internals, how is it possible that loader takes on existing authorizer or switches to a new one.

Community
  • 1
  • 1
Little Alien
  • 1
  • 8
  • 22
  • For our app we generally preemptively refresh the auth token before it has a chance to expire, you can do this using the expires_in field returned on the token." – Mr.Rebot Jun 08 '16 at 06:06
  • 1
    Thanks, Mr Rebot. This helped me in the past because I had an error in the `case TOKEN_REFRESH_REQUIRED: rtclient.authorizer.authorize()` -- I forgotten the `break` at the end and fallen into `CLIENT_ERROR`, which fired error and broke something in the API. So, I dropped the refresh since that since it makes no sense for me. I am still curious to know how does loader check that authorizer is authorized. BTW, it could do the authorization and refresh itself in this case. – Little Alien Jun 08 '16 at 11:18
  • Based on this thread : [Google Drive Realtime API OAuth2 Refresh Errors](http://stackoverflow.com/questions/17623682/google-drive-realtime-api-oauth2-refresh-errors), [Google Drive Realtime API OAuth2 Refresh Errors (Part 2)](http://stackoverflow.com/questions/17896381/google-drive-realtime-api-oauth2-refresh-errors-part-2),[Google Drive Realtime API OAuth2 Refresh Errors (Part 3)](http://stackoverflow.com/questions/19415183/google-drive-realtime-api-oauth2-refresh-errors-part-3). In general, the Realtime API should reconnect, even if it has been idle for hours or the computer has been asleep. – Mr.Rebot Jun 08 '16 at 11:34
  • In general, the Realtime API should reconnect, even if it has been idle for hours or the computer has been asleep."When the token_refresh_required is fired you should handle the error by calling gapi.auth.authorize again with immediate:true. (If you're using gapi.auth.signIn for authorization you probably want to use that instead.) – Mr.Rebot Jun 08 '16 at 11:35
  • It will ask you to reconnect with REFRESH_REQUIRED. I do not see any reason for keeping connection alive, especially with DocumentSavingStateChanged visible to the user. – Little Alien Jun 08 '16 at 12:47
  • @Mr.Rebot I see that the need to refresh may appear once you get 500 Internal Error in response. It seems that client dies in case and forced authorization could probably reactivate it. – Little Alien Sep 09 '16 at 20:35