Question

I use Youtube API v3 with jasvascript

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list?part=statistics&forUsername=GameSprout&_h=8&

And i have this simple code

<script type="text/javascript"> 
    function googleApiClientReady() {
        var viewCount, channelName, subscribers;
        gapi.client.load('youtube', 'v3', function() {
            var request = gapi.client.youtube.channels.list({
                part: 'statistics',
                forUsername : 'GameSprout'
            });
            request.execute(function(response) {
                viewCount = response.result.items[0].statistics.viewCount;

            });
        });
    }
</script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>

But i have this error :

TypeError: response.result is undefined

How can fix this problem ?

EDIT

the reponse of :

console.log(response)

IS :

Object {code: 403, message: "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", data: Array[1], error: Object}
code: 403
data: Array[1]
error: Object
message: "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
__proto__: Object
Was it helpful?

Solution

You haven't set your API key, which is needed for read-only (i.e. without oAuth) calls. Before calling gapi.client.load you must do this:

gapi.client.setApiKey("YOUR_KEY_HERE");

See:

https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientsetApiKey

for documentation.

You can also use the key parameter on the object passed to your request if you'd rather, but if you're ever doing more than a single call you might as well just set it on your client instance.

Here's a quick intro for obtaining said API key from the cloud console:

https://plus.google.com/+YouTubeDev/posts/BBkwPXP94JB

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top