gapi is not defined - Google sign in issue with gapi.auth2.init

后端 未结 6 1513
死守一世寂寞
死守一世寂寞 2020-11-29 05:07

I\'m trying to implement Google Sign In and retrieve the profile information of the user. The error is: Uncaught ReferenceError: gapi is not defined. Why is that?



        
6条回答
  •  無奈伤痛
    2020-11-29 05:34

    I think you'll find with the above example that this won't work either, as gapi.auth2 will not yet be defined (I know this because I made the same mistake myself, today) You first need to call gapi.load('auth2', callback) and pass THAT a callback which then calls gapi.auth2.init. Here is an example of my _onGoogleLoad function, which is the callback for loading the first platform.js script.

    var _auth2
    
    var _onGoogleLoad = function () {
      gapi.load('auth2', function () {
        _auth2 = gapi.auth2.init({
          client_id: 'OUR_REAL_ID_GOES_HERE',
          scope: 'email',
          fetch_basic_profile: false
        })
        _enableGoogleButton()
      })
    }
    

    After that, you can use the _auth2 variable to actually sign users in.

提交回复
热议问题