Are you in the process of developing an application designed to incorporate one one or more of the Google APIs? Do you already have an application and want to add Google data to it? Before you can access most of the Google APIs you must register your project on Google Developers console.
This post number three in my six part beginning Google Development series, which started with the post Google Development for beginners and continued with registering a project with Google. Now we are going to build upon our new project on Google Developers console by telling Google which APIs we intend to access.
In this post we will look at adding APIs to our new project on the Google Developers console. If you try and access an API without adding it to your project on Google Developers console you will get an error so it is import to remember this step.
We will also be discussing quota and billing for projects.
Let us get started ….
If you haven’t already created a project on Google Developers console you can go and check the previous post in this series entitled registering a project with Google. In the registering a project with Google post we covered.
- Why we need to create a project on Google Developers console.
- How to create a project the project.
- Critical settings needed to ensure we don’t receive any errors from Google when we make our requests.
As I stated in the first post in this series Google development for beginners. Google has an extensive list of APIs that they have exposed for public use Most of the Google APIs we are free to use. Google doesn’t charge us to access most of their APIs. However there are a a few of the Google APIs that you will have to pay to use or pay to use more then the default public quota.
The first step in accessing them was to create our project on Google Developers console which we did in the previous post in this series registering a project with Google. You should now have a project on Google Developers console ready to add APIs to it.
Note: If you attempt to access a Google API using a project that the API has not been enabled for you will get an error back from the API.
Access Not Configured. Please use Google Developers Console to activate the API for your project.
Lets look at how to add an APIs to our project on Google Developers console.
Adding APIs to a project
In the Google Developers console under the APIs & auths menu you will find the APIs menu. On this page you will find a list of all the most popular Google APIs that you can access. You can also search for an API if you know the name, personally I find it easier to just search for the API I am interested in.
Find the YouTube data API and click on it.
I will be using the YouTube Data API and the Google Calendar API in this series if you intend to continue along you can enable the Google Calendar API now as well you will need it later on in this series.
Once you are on the new screen for your the API you can click on the blue Enable API button. This will add the API to your project. All enabling an API does is tell google that you intend to use the API in your project.
There is a tab at the top of this screen Overview, Usage and Quotas
Warning: If at some point you apply for an extension of your Quota and you disable this API you will loose the quota extension and have to reapply for it again. Do NOT disable an API and re-enable it. This is a very dangerous button.
All overview does is give you information about the API you have enabled here we see it says:
The YouTube Data API v3 is an experimental API that provides access to YouTube data, such as videos, playlists, and channels.
So if we want to access say Google drive we know that this wont help us because its only giving us access to YouTube Data.
Usage tells you how many requests you are have made to the API.
In my experience this is not every accurate. My own logging of this project tells me that these numbers are not correct. Google has been working on this screen for as long as I have been developing with them, this screen is better then what we had back then. My only hope is that some day we will get something that is realtime and accurate.
The next tab gives you information about the default quota allowance for the API. By default you are allowed to make X number of request against the API each day. Most of the quotas you can apply for an extension, of the project based quotas but not the user based quotas. However it is important to note that you may have to pay for the extension of your quota. You should always check the API you are working with to find out what quotas apply to it.
It is a good idea to click on the little pencil next to the per-user limit and set it as high as you can. The amount you can set it to will depend upon the API you have enabled its best to check the documentation to see what the max limit is.
Requests a day is a little misleading here, it makes you think that a single request counts as one against your quota limit. This is not always the case some APIs, the YouTube API for example have different costs.
I know I covered quota in the first post in this series but I am going to extend the information I already gave you here. But before I do we need to be clear on one thing.
What is a request?
A request is any message sent to the API.
- List most popular YouTube videos is a request,
- List my files on Google drive, is also a request.
APIs normally return a max result set per request of say 50 rows, if there are more then 50 rows then a second request must be made to get the next 50 rows. Each request to get the next set of rows is a request. 1000 rows at 50 row chunks, you can quickly eat though your quota. This is why its important to set Maxrows as high as you can without slowing your application down. It wont cost you any more to make one request for 1000 rows, but 1000 / 50 = 20 quota cost.
Some requests cost more then just one against your quota. The YouTube API also charges differently you may only make one request but it could cost you more then one against your quota. The documentation for the method will tell you if the quota cost is more then just one. Below is an example from the YouTube API documentation. (Now you know why chose the YouTube API for this post)
The list below contains the part names that you can include in the parameter value and the quota cost for each part: contentDetails: 2, fileDetails: 1
Note! When I say cost and charge I do not mean money. This is against your allotted quota. Google is not going to charge you unless you accept there billing terms you wont suddenly receive a bill from Google to pay them.
What is quota?
Quota is the number of requests you are allowed to make to a Google API without requesting permission to access more. Most of the Google APIs have a free quota, a number of requests Google lets you make without asking for permission to make more request. You can see the free quota you are allowed with the YouTube API in the picture.
It does not always cost you anything to extend your quota it depends upon the API if you are going to have to pay for permission to make additional requests. You will have to apply for a quota extension sometimes it doesn’t take very log to get the extension, but it can take up to a month so its a good idea to keep an eye on your quota usage if you reach 80% of your allowed quota its a good idea to request an extension at that time.
Unless it says other wise APIs Quotas are projects based not user based.
Each API has different quotas I encourage you to check the API you are using for information about the quota it has available the following examples are from the YouTube API.
Project quota example
If I authenticate the application and make 10,000,000 requests then you authenticate the application and make 40,000,000 requests the quota limit will have been reached and any additional request will result in the following error.
dailyLimitExceeded A daily quota limit for the API has been reached.
You will have to wait until midnight Pacific Standard Time(PST) before you can make more requests.
Google project Quotas reset at midnight Pacific Standard Time(PST) this is the west cost USA timezone.
Personally I think it would have been more logical to reset it at GMT time but Google is located in the PST time zone so maybe its easier for them to understand.
User quota example
Per-user limit 3,000 requests/second/user
Some Quotas are user based, a user is normally the person that has authenticated the request so it is hard to understand how it can come into play with a public API key, a service account and OAuth2 but it does. Every request sent to google contains information about who is making the request in the form of the IP address where the address came from. If you have your code running on a server the IP address is the same all the time so Google sees it as the same user. You can get around his by adding Quotauser to your request this will identify the request based upon different users.
If you send to many requests to fast from the same user you will see the following error.
userRateLimitExceeded The request failed because a per-user rate limit has been reached.
The best way to get around this is to use QuotaUser in all of your requests, and identify different users to Google.
Adding an API to our project on Google Developers console tells Google which API we intend to access. If you don’t enable an API and try to access it you will get a Access Not Configured. Please use Google Developers Console to activate the API for your project error from Google. There are two types of quota associated with Google APIs, project based quota and user based quotas. Project based quotas reset at midnight PST time. Remember to check the quota cost for the requests you make against API and to apply for an extension of your quota when you reach 80% usage of the current quota.
QuotaUser and Maxrows are two things to think about when making your requests to the Google APIs.
In the next post in this series we will look at public data and how to create a public API key to access public data on a Google API with our project.
That for your clear and build articales. You are very high ability and talent explaining things an making them simple.
I have the question: if a user gets to my site from Google, can I know in real time where he came from and w hi at search term did he search for in Google?
There is a different with the above between a user that authonti authonticated to my site with his Google account to other users?
Google analytics would be able to tell you who is visiting your website and where they came from.