简体   繁体   中英

Google+ api OAuth 2.0 : how to access token object 'authResult' from .js file

I have a HTML/CSS/JS app using Jquery Mobile, in which I would like to create a sign-up page.

The function signinCallback in the .html perfectly finds the variable called authResult (OAuth 2.0 Token Object), but in the .js authResult is undefined and therefore the function signinCallback doesn't work (see info on this function here : https://developers.google.com/+/web/signin/disconnect ) nor other api's methods like gapi.auth.getToken(); .

I get the following errors from the .js file :

ReferenceError: gapi is not defined

ReferenceError: authResult is not defined Can you help me figure out why ?

Thanks.

The .html:

    <!-- SIGN-IN PAGE -->
<div id="signin-page" data-role="page" data-add-back-btn="true" data-back-btn-text="Back">
    <header data-role="header">
    </header>
    <div id="contentsignin" data-role="content">                
        <script type="text/javascript">
            function signinCallback(authResult) {
            console.log(' --signinCallback in script in .html');
                  if (authResult['status']['signed_in']) {
                  } else {
                  }
                }
    </script>
    <span id="signinButton">
          <span
            class="g-signin"
            data-callback="signinCallback"
            data-clientid="myclientid"
            data-cookiepolicy="single_host_origin"
            data-requestvisibleactions="http://schemas.google.com/AddActivity"
            data-scope="https://www.googleapis.com/auth/plus.login">
          </span>
        </span>
        <button type="button" id="revokeButton">
            Revoke token...
        </button>
        </div>
 </div>


...

    <script type="text/javascript">
        (function() {
            var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
            po.src = 'https://apis.google.com/js/client:plusone.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
        })();
    </script>

The .js:

    var testtest = gapi.auth.getToken();
    $('#revokeButton').click(disconnectUser);

console.log(authResult);

function disconnectUser(authResult) {
    var access_token = authResult['access_token'];
      var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
         access_token;
      // Perform an asynchronous GET request.
      $.ajax({
        type: 'GET',
        url: revokeUrl,
        async: false,
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(nullResponse) {
         // Do something now that user is disconnected
         // The response is always undefined.
         document.getElementById('loginstatusin').setAttribute('style', 'display: none');
         document.getElementById('loginstatusin').setAttribute('style', 'display: block');
        },
        error: function(e) {
         // Handle the error
        }
      });
    }

Its a little bit late answer but I think you need to first download js script which contains authResult and then download your own .js file so that when code in your .js file is executing, authResult related code is already downloaded and present for your code.

<script src="path\to\js_file\containing\authResult"></script>
<script src="path\to\your\js_file.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM