ngGAPI icon indicating copy to clipboard operation
ngGAPI copied to clipboard

Cannot read property 'access_token' of undefined

Open FaisalAl-Tameemi opened this issue 9 years ago • 17 comments

Error when trying to use GAPI.init();

Already checked if the value 'GoogleApp' is defined at the module level.

Tried the sample app wich uses ngDropbox and ngGapi, same problem. Not sure what to fix. Your help is appreciated.

FaisalAl-Tameemi avatar Apr 08 '15 04:04 FaisalAl-Tameemi

I got the same error TypeError: Cannot read property 'access_token' of undefined at oauthHeader (http://localhost:8100/lib/ngGAPI/gapi.js:113:73) at request (http://localhost:8100/lib/ngGAPI/gapi.js:129:7) at GAPI.get (http://localhost:8100/lib/ngGAPI/gapi.js:170:14) at GAPI.Youtube.search (http://localhost:8100/lib/ngGAPI/gapi.js:455:22)

clowNay avatar Apr 28 '15 09:04 clowNay

Same Error :(

hughred22 avatar Apr 29 '15 21:04 hughred22

screen shot 2015-05-10 at 9 08 51 pm

Anyone found a fix?

divir94 avatar May 11 '15 02:05 divir94

I'm also getting same error. Did you guys got any fix for this? Kindly respond..

itsanmax avatar May 27 '15 10:05 itsanmax

I'm facing the same issue.

At first, I tried the simple $scope.videos = Youtube.search(...) in my controller. Then I noticed the GAPI.init() method in the source, which sets the oauthToken variable. So I decided to wrap the search call like this -:

GAPI.init()
  .then(function(){
    $scope.videos = Youtube.search({part: 'snippet', q: 'Ayy lmao'});
  }, function(){ console.log('Something went wrong yes?'); });

On doing this, I get the following stack trace -:

TypeError: Cannot read property 'authorize' of undefined
    at Function.GAPI.init (http://localhost:8100/lib/ngGAPI/gapi.js:409:16)
    at new <anonymous> (http://localhost:8100/js/controllers.js:15:10)
    at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:12884:17)
    at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:12892:27)
    at http://localhost:8100/lib/ionic/js/ionic.bundle.js:17161:28
    ....
    ....

All this boils down to the same problem that I was trying to avoid before using this library - it appears that the gapi object is not yet populated, and so gapi.auth turns out to be undefined.

The issue remains the same even if I try to keep the client.js script on my local. Unless gapi finishes loading, its properties will be undefined - ref: http://stackoverflow.com/questions/12874491/why-is-gapi-client-from-google-plus-api-undefined

I've issued a hack in my ionic app to get this to work. Basically, we cannot avoid the onload parameter in the JS.

 <!-- Hack because we can't avoid this shitty onload parameter -->
 <!-- Need to think of better way -->
 <script type="text/javascript">
   function onClientLoad(){
     gapi.client.load('youtube', 'v3', function(){
       console.log("Setting API key");
       gapi.client.setApiKey('your-api-key');
     });
   }
 </script>
 <script src="https://apis.google.com/js/client.js?onload=onClientLoad"></script>

 <!-- ionic/angularjs js -->
 <script src="lib/ionic/js/ionic.bundle.js"></script>

 <!-- ngGAPI script -->
 <script src="lib/ngGAPI/gapi.js"></script>

The order of the script tags is very, very important. First we define what we do when the client script loads completely. Then we have the line for the client script.

Then we begin our regular angular dependency lineup. A note to ionic users: refreshing your app in the browser (during development) is not sufficient - you need to re-serve the app to avoid the undefined errors - probably because of ionic's caching.

Further note to the poor souls who got all those access denied errors despite having set the client ID and the API key properly - Google no longer offers the free edition of Google Apps. To register your app with them and define the API scopes that you can use, you'll have to pay for an account. ref: http://stackoverflow.com/questions/21452842/access-denied-requested-scopes-not-allowed-for-google-service-account

PritishC avatar Jun 06 '15 08:06 PritishC

Seeing the exact same issue, tried changing the loading of the scripts but no luck

maallen avatar Jan 24 '16 22:01 maallen

Any updates on this issue? Having the same problem.

arielcr avatar Feb 15 '16 20:02 arielcr

Problem is gapi (from google api client library - https://apis.google.com/js/client.js) not fully loaded when GAPI.init invoked. I create pull request https://github.com/christiansmith/ngGAPI/pull/19 with wrapped GAPI.init into gapi.loaded method

nastradamus39 avatar Mar 04 '16 21:03 nastradamus39

So this fixes the problem @nastradamus39 ?

arielcr avatar Mar 04 '16 21:03 arielcr

@arielcr Yep. But i catch "Cannot read property 'access_token' of undefined" because of bad keys. access_token set only after success response - https://github.com/christiansmith/ngGAPI/blob/master/gapi.js#L386

nastradamus39 avatar Mar 04 '16 22:03 nastradamus39

Already i catch another strange bug. In gapi.auth.authorize({ client_id: app.clientId, scope: app.scopes, "immediate": true }, onAuth );

If "immediate" set to true - authorization popup not shows. And we got error above. If "immediate" set to false - all ok.

nastradamus39 avatar Mar 04 '16 22:03 nastradamus39

Still not working using your fix @nastradamus39

arielcr avatar Mar 07 '16 18:03 arielcr

@arielcr I had the same issue after implementing @nastradamus39 's solution but then tried with an Incognito window and it worked. After that I tried with a hard reload and it seems to be working in a regular window.

I recommend immediate: true for the initial authorize call and immediate: false for the one inside the onAuth callback.

ChristianMMacy avatar Mar 22 '16 19:03 ChristianMMacy

Hi, I'm working on an Ionic Project and I try to use this plugin, but I have the same problem ... This is the same with @nastradamus39's changes. Thank you Guillaume

guirou62 avatar May 18 '16 16:05 guirou62

Google changes the name of the property that holds the object with the access_token from hg to Zi. If you are accessing that directly you should change your code, for example

var objectWithAccessToken = res.hg || res.Zi;
this.onAuthResult('Google', objectWithAccessToken.access_token);

And yes I know we are not supposed to touch that object directly in the first place.

ofirgeller avatar Aug 31 '16 17:08 ofirgeller

the response has now changed to "res.uc.access_token"

jinghanng avatar Feb 27 '20 12:02 jinghanng

@sariputta Thanks, I have a legacy system that depends on this that I need to keep running for a few more months.

ofirgeller avatar Feb 27 '20 12:02 ofirgeller