Over the last couple days I've been seeing some increasingly problematic issues with a long running realtime app. I've walked through the steps below and the code snippet at the bottom contains additional debug info from the app.
When the page first opens it successfully requests an OAuth token and loads the realtime doc [A]. After 50 minutes (10 minutes before the token expires) it re-requests a new OAuth token successfully [B]. After the first token has expired the currently open connection gets a 401 unauthorized error and requires a new oauth token [C]. This in itself seems like a problem as it should have updated itself to use the new valid token from [B].
However the app should still be resilient to an error like this occurring - so it gets handled by closing and reopening the doc [D] and getting another new OAuth token. Unfortunately at this point, the realtime API sits in an infinite loop getting errors for the access_token [E].
All of the OAuth tokens are requested using gapi.auth.authorize with the same scopes and no call to setToken. I previously tried using setToken, but that had exactly the same problems.
Actual Questions What is the correct way to deal with refreshing the OAuth token for the realtime API? How can I prevent repeated failures internal to the drive APIs when closing and reopening a doc?
[A]
_aa: "1"
access_token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXz9AYBkyympssqI"
client_id: "XXXXXXXXXXXXXXXX.apps.googleusercontent.com"
cookie_policy: undefined
expires_at: "1373610287"
expires_in: "3600"
g_user_cookie_policy: undefined
issued_at: "1373606687"
response_type: "token"
scope: Array[2]
state: ""
token_type: "Bearer"
[B]
_aa: "1"
access_token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXV2kzG4EMUppi"
client_id: "XXXXXXXXXXXXXX.apps.googleusercontent.com"
cookie_policy: undefined
expires_at: "1373613288"
expires_in: "3600"
g_user_cookie_policy: undefined
issued_at: "1373609688"
response_type: "token"
scope: Array[2]
state: ""
token_type: "Bearer"
[C]
GET https://drive.google.com/otservice/bind?id=1B-XXXXXXXXXXXXXXXXXXXXX_nRizfqmT…&RID=rpc&SID=XXXXXXXXXXXXXXXXX&CI=0&AID=221&TYPE=xmlhttp&zx=ns6e5dr7rf4&t=1 401 (Unauthorized)
Drive Realtime API Error: token_refresh_required: The OAuth token must be refreshed.
[D]
[Close Realtime Document]
[Open Realtime Document]
_aa: "1"
access_token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXMHzJXm2dF-"
client_id: "XXXXXXXXXXXXXX.apps.googleusercontent.com"
cookie_policy: undefined
expires_at: "1373613918"
expires_in: "3600"
g_user_cookie_policy: undefined
issued_at: "1373610318"
response_type: "token"
scope: Array[2]
state: ""
token_type: "Bearer"
[E]
[x100] Uncaught TypeError: Cannot read property 'o' of null
Thanks!