简体   繁体   中英

TypeError: gapi.auth2.grantOfflineAccess is not a function

I am initializing gapi.auth2 in my index.html

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="google-signin-client_id" content= "817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com">

        <meta name="google-signin-scope" content="profile email">
        <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
        <script>
            function start() {
                console.log('script running')
                gapi.load('auth2', function() {
                    auth2 = gapi.auth2.init({
                        client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com',
                        scope: 'profile email'
                    });
                });
            }
        </script>

    <!-- <meta name="theme-color" content="#000000">     -->
    <!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> -->
    <!-- <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> -->

    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>


  </body>
</html>

Then in my react app:

At the top of the page: /* global gapi */

    handleSignIn() {
        console.log(gapi.auth2)
        gapi.auth2.grantOfflineAccess()
    }

the console.log is printing:

{init: ƒ, authorize: ƒ, _gt: ƒ, enableDebugLogs: ƒ, getAuthInstance: ƒ, …}

so it looks like gapi.auth2 is a function, but I'm getting the error:

gapi.auth2.grantOfflineAccess is not a function

What am I doing wrong?

It looks like you are not calling grantOfflineAccess on the GoogleAuth object. To get the GoogleAuth object, you need to call getAuthInstance() like so:

var ga = gapi.auth2.getAuthInstance();

ga.grantOfflineAccess(); 

Make sure you only call getAuthInstance() after first calling gapi.auth2.init() as specified here .

Missed a step:

looking at the function that was printed I needed to call another function first:

gapi.auth2.getAuthInstance().grantOfflineAccess()

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