How can i know that my Youtube API Data is correct?

167 views Asked by At

I having some trouble to understand something related to the API of youtube So my code is basically very simple:

name  = input("enter the username: ")
key = "MY API KEY"


data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels? 
part=statistics&forUsername="+name+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]

print(name + " has " + "{:,d}".format(int(subs)) + " subscribers!")

just yelling the number of subscribers after giving specific YouTube Username: The thing is that some Usernames(for example: Vsuase/Veritasium/Unbox Therapy ) which have many subs and the API-URL giving me wrong Data

Vsause - in return giving me back 72 subs

Veritasium/Unbox Therapy - not giving my any number at all

BUT, a channel "Computerphile" giving me that exact same subs they have.

How come that few Usernames work and few do not??

2

There are 2 answers

1
Mauricio Arias Olave On

I tested in both, using the try-it functionality available in the YouTube Data API - Official Documentation and in the Google API Explorer and in both sites the results are closely1 similar.

For example, when the statistics of the YouTube user vsauce is requested vía YouTube API, the value in subscriberCount is 14220819 and checking his YouTube channel it says: 14,220,825.

Here is the example for request the statistics of the YouTube user vsauce (using the try-it)

And here is the demo for request the statistics of the YouTube user vsauce (using the Google API Explorer).

I didn't see any differences in the values in subscriberCount by requesting the other channels you mentioned in your question.


1 You need consider that some channels has more changes in the quantity of subscribers than others and such results vary too in the responses of the API.

0
Ben Koren Kruiger On

For some reason, if you change in the URL from forUsernae= --> id= it gives you the correct numbers.

TED channel: https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCAuUUnT6oDeKwE6v1NGQxug&key=AIzaSyDjnINqqxQlIg4kbXoPDVYOhHNfdmDbb9I