0

My app interacts with Google with JavaScript only. I want user to access their files list without login (access offline) after first time authentication. I tried many solutions available on stack or in google documentation. But Not able to get any solution. so I has uploaded the fresh running code. But this is asking the user for login every time when he is not logged in to gmail in browser.

My html is:

<button id="authorize-button" style="display: none;">Authorize</button>
<pre id="content"></pre>

My Script :

 <script type="text/javascript">

  var CLIENT_ID = '****************';
  var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
  var SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly';
  var authorizeButton = document.getElementById('authorize-button');
  function handleClientLoad() {
    gapi.load('client:auth2', initClient);
  }
  function initClient() {
    gapi.client.init({
      discoveryDocs: DISCOVERY_DOCS,
      clientId: CLIENT_ID,
      scope: SCOPES
    }).then(function () {
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
      authorizeButton.onclick = handleAuthClick;
    });
  }

  function updateSigninStatus(isSignedIn) {
    if (isSignedIn) {
      authorizeButton.style.display = 'none';
                listFiles();
    } else {
      authorizeButton.style.display = 'block';

    }
  }

  function handleAuthClick(event) {
    gapi.auth2.getAuthInstance().signIn();
  }

  function appendPre(message) {
    var pre = document.getElementById('content');
    var textContent = document.createTextNode(message + '\n');
    pre.appendChild(textContent);
  }

  function listFiles() {
    gapi.client.drive.files.list({
      'pageSize': 10,
      'fields': "nextPageToken, files(id, name)"
    }).then(function(response) {
      appendPre('Files:');
      var files = response.result.files;
      if (files && files.length > 0) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          appendPre(file.name + ' (' + file.id + ')');
        }
      } else {
        appendPre('No files found.');
      }
    });
  }

</script>
<script async defer src="https://apis.google.com/js/api.js"
  onload="this.onload=function(){};handleClientLoad()"
  onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>

I also tried to user:

var auth2;
function init() {
alert('in init');
gapi.load('auth2', function() {
    auth2 = gapi.auth2.init({
        clientId: '**********************',
        scope: 'https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appfolder'
    });
});
$('#authorize-button').click(function() {
    auth2.grantOfflineAccess({'redirect_uri' : 'postmessage', 'approval_prompt' : 'force'}).then(function(resp) {
    console.log(resp);
    var auth_code = resp.code;
});
});
}

Here I am able to access the response code. But how to use it for offline access. Please help me with some code. I also has tried almost all documentation and got very confused.

techie
  • 13
  • 6
  • 1
    I don't think Offline access is allowed for server-less clients (they use Implicit flow). Offline access works with Authorization Code flow which typically requires a server to ensure the auth-code is not leaked. Server-less clients are prone to leaking the code (e.g. through XSS attacks), so OAuth providers don't allow Authorization Code flows for them. – hazardous May 10 '17 at 06:51
  • thanks Hazardous. You are correct. But there should be some procedure for this. I got an hint regarding this at http://stackoverflow.com/questions/10330992/authorization-of-google-drive-using-javascript/10331393#10331393 please check and help. I am also trying to do this. Thanks for your time friend. – techie May 10 '17 at 07:07
  • One possibility is that your App uses a cloud deployed microservice to provide minimal set of server implementation which can take care of the Authcode flow. – hazardous May 10 '17 at 07:40
  • Possible duplicate of [Authorization of Google Drive using JavaScript](http://stackoverflow.com/questions/10330992/authorization-of-google-drive-using-javascript) – Linda Lawton - DaImTo May 10 '17 at 09:03
  • hi Dalm do you help me with some code – techie May 10 '17 at 09:42

0 Answers0