Monday, 15 July 2013

javascript - Get new Google OAuth Token when old one expires? -


I'm developing a Chrome extension that uses Google's OAuth to get the user's name and in Google Spreadsheets Can write something .

I was able to do whatever I wanted, but I noticed that every time I open the page action of the extension (where it is being used), it becomes new and Gives a new token to my extension even though the old one is still not finished.

Ideally I need to do some sort of investigation and before the old one expires, a new one is released. At present, if a user waits for more than 1 hour, the extension becomes useless because it is still trying to use the old token.

So my question is how can I refresh it and give a new token to the extension when it is just about to end a new token? I'm not trying to manually click the user manually every 1 hour, so I can not understand it once.

Here's the code that I want to hide:

popup.html:

  

contentcript.js:

  (function () {var p = document.createElement ('script'); po .type = 'text / javascript'; po.async = true; po.src = 'https://apis.google.com/js/client:plusone.js?onload=signin callback'; var s = document.getElementsByTagName ('Script') [0]; s.parentNode.insertBefore (PO, S);}) (); Function signinCallback (authResult) {if (authResult ['status'] ['signed_in']) {// Hide your welcome text and sign in sign in button Document.getElementById ('signinbutton') setAttribute ('style', 'Do not display anything'); Document.getElementById ('Please log in). SetAttribute ('style', 'display: none'); // I am storing local tokens in chrome. Chromelocals used by the rest of the extension. Storage (gapi.auth.getToken (). Access_token); Chrome.storage.local.set ({'token': token}); // get logged in user name makeAPICall (); // Reload the current tab so that they can choose the user's name and their token chrome.tabs.reload (); } Else {// If the user is not logged in or have any problems, then find out to log in to the document. GetElementById ('Please log in'). SetAttribute ('style', 'display:'); Console.log ('Sign In Issue:' + authResult ['Error']); }} // Request information related to user accounts such as their name makeapicall (function) {gapi.client.load ('plus', 'v1', function () {var request = gapi.client.plus.people.get () {'UserId': 'me'}); Request.execute (function) {var username = resp.displayName; chrome.storage.local.set ({'username': username});});}); }  

I am thinking that I would have to somehow check whether the current token has expired or is due, then get a new one and finally save it to Chrome. Local. Storage extensions require it next time.

I also tried Chrome. Identification and issuing a new token, but it can not be used until the entire extension is reloaded. /

Thanks!


No comments:

Post a Comment