ngGAPI
ngGAPI copied to clipboard
Cannot read property 'access_token' of undefined
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.
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)
Same Error :(
Anyone found a fix?
I'm also getting same error. Did you guys got any fix for this? Kindly respond..
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
Seeing the exact same issue, tried changing the loading of the scripts but no luck
Any updates on this issue? Having the same problem.
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
So this fixes the problem @nastradamus39 ?
@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
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.
Still not working using your fix @nastradamus39
@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.
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
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.
the response has now changed to "res.uc.access_token"
@sariputta Thanks, I have a legacy system that depends on this that I need to keep running for a few more months.