Youtube Quota on new Project - google-apps-script

I am using the Youtube API with Apps Script for the first time but I get a quota error. I activated the Youtube API from the Apps Script window, and added my scripts.
Note I am using a Google Workspace account. I didn't have this problem with my private google account.
Here is the code that generates the error:
function getVids() {
var list = YouTube.Search.list('id,snippet', {
channelId: 'UCYf_kU_HoMOUUe3hW-0ou5Q',
maxResults: 25
})
var json = JSON.parse(list);
var items = list.items;
var allVideos = [];
for (var i=1; i<items.length;i++) {
var videoId = list.items[i].id.videoId;
allVideos.push(videoId);
}
console.log(allVideos)
}
I receive this error:
GoogleJsonResponseException: API call to youtube.videos.insert failed
with error: The request cannot be completed because you have exceeded
your quota.
I tried looking at the consumption here: https://console.developers.google.com/apis/dashboard but it redirects me to https://console.developers.google.com/apis/dashboard?project=northern-math-286006&folder=&organizationId= which I assume is some default project name as I didn't choose it. I selected https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?folder=&organizationId=&project=northern-math-286006 from this page to view Youtube data but it returns "No data is available for the selected time frame."

Explanation:
According to the official documentation you have exceeded 10,000 units per day.
Unit points might be a little tricky to understand but there is a very good explanation on the link above itself:
You are doing SEARCH requests and for every search request you spend 100 units. I assume you have executed this code and/or other codes that consume points and this is why you reached your maximum unit points.
According to the documentation here you can see your quota usage but you might need to have permission to access that info.
YouTube Data API (v3) - Quota Calculator:
If your application calls a method, such as search.list, that returns
multiple pages of results, each request to retrieve an additional page
of results incurs the estimated quota cost.

Did you check your quota for this API? You can find your quotas under 'IAM &A Admin' -> Quotas. Then look for 'YouTube Data API V3'.
Click on the one saying 'Queries per day' and see if the Limit is set to 0.
If yes, you might try to disable this API and enable again.
I had this issue too, and after disabling and enabling the API I get the 10k units.

Related

With what frequency is my script calling the API? I don't want to get cut off

I'm using a json script from another stack overflow post and don't know with what frequency I'm calling the external data (from Seatgeek).
I don't want to get cut off from the Seatgeek api so I want to make sure I'm not going to bog down the system - I really only need the data to refresh twice a day.
I would not consider myself a developer so I'm not sure where to look. Can someone please help by taking a look at the script?
According to the Google Sheets API documentation:
Google Sheets API has a limit of 500 requests per 100 seconds per project, and 100 requests per 100 seconds per user. Limits for reads and writes are tracked separately. There is no daily usage limit.
Also, if you want to find the usage and more statistics regarding the API, you can check the Google API console and find all the information there.
For more information regarding your issue, you could check the following documentation:
[1] Google Sheets API;
[2] Google Console API Quota

How many service calls am I allowed in a day?

I've set up two sets of address. One across the second row starting at B2 and another down the first column starting at A3. Using a custom function invoking google's api I am trying to get a list of drive times in minutes for all locations in the first column to all locations in the second row. I get an error stating that it has ran to many times in one day. Here is the custom function
function DriveTime(origin, destination) {
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.getDirections();
return directions.routes[0].legs[0].duration.value/60;
}
Error: Service invoked too many times in one day: route (line 2)
I understand the error, but how many times in one day can I make this call as a standard free user. And where can I find pricing to increase the amount of daily calls to this service?
Also if anyone knows of a way to accomplish this in less calls than I am attempting, that'd be welcomed knowledge as well.
Technically, your quota as a apps script user without API key/Billing information should be zero. But for whatever reason, Google didn't make changes to charge apps script Maps library when they changed the pricing back in June'18 for everything else related to Maps API. There seems to be a default quota, which is not explicitly mentioned in apps script documentation pages.
If you have had premium plan in the past, you can leverage Maps.setAuthentication(). But if you do not,
This method doesn't work with API keys. Additionally, please note that Premium Plan is no longer available for new customers. If you don't already have a Premium Plan license, please don't call setAuthentication(clientId, signingKey). You are able to use the Maps methods with the default quota allowances.
Which means you cannot extend the default quota allowance.
Your alternative would then be to call the maps api directlyskip oauth using urlfetchapp and parse the response using apps script vanilla javascript(es5).
You can see the pricing here.

How to add my key to this google app script?

I need to update this script to pass my key so that I don't go over the limit per day. How would I modify this script to pass my key?
(NOTE: google class information found here: https://developers.google.com/apps-script/reference/maps/geocoder)
function geo2zip(a) {
var response=Maps.newGeocoder()
.reverseGeocode(lat(a),long(a));
return response.results[0].formatted_address.split(',')[2].trim().split(' ')[1];
}
function lat(pointa) {
var response = Maps.newGeocoder()
.geocode(pointa);
return response.results[0].geometry.location.lat
}
function long(pointa) {
var response = Maps.newGeocoder()
.geocode(pointa);
return response.results[0].geometry.location.lng
}
I have never used a google apps script before.
I have this script in place and am using "=geo2zip(cell)" to call the script from my google sheet to get the zip code for incomplete addresses. There are ~28k in my sheet. I have enabled the geocoding api in my google console and my billing info. and created my key but am not sure how to include my key in the script above.
I have also tried using the following to call the api using my key. It is working, but this has resulted in VERY slow responses. At the rate it is taking these to respond, it will take me 10 days to finish updating my 28k records.
CELL M4852--> "https://maps.googleapis.com/maps/api/geocode/xml?address=ADDRESS&key=MYKEY"
NEXT CELL--> "=ImportXML(M4852,"/GeocodeResponse/result/formatted_address")"
The script responds much more quickly, so I would prefer to use that and pass my key. Please let me know if you can help.
UPDATE: I was able to resolve this using Alberto's suggestion below of adding the Maps.setAuthentication.
I believe your issue has more to do with the Google API management than with the scripts themselves. There is a way to limit how much your key is used. According to the Maps Platform documentation:
Manage Your Cost of Use
To manage your cost of use of the Google Maps
Platform APIs, you can set daily limits to all requests to any
billable API.
To view or change daily billable limits for the Geocoding API, do the
following:
Go to the Geocoding API Quotas page in the Google Cloud Platform
Console. From the projects list, select a project. In the Requests
section, on the Requests per day line, click the edit icon, then enter
the preferred total billable daily quota, up to the limit (if any)
specified by Google.
You can basically set how many requests you want to allow per day, which will avoid you going over your limit.
UPDATE
You can link the script to your account using the Maps.setAuthentication(clientId, signingKey) method, according to the docs, it:
Enables the use of an externally established Maps API for Business
account, to leverage additional quota allowances. Your client ID and
signing key can be obtained from the Google Enterprise Support Portal.
Set these values to null to go back to using the default quota
allowances.
Map Documentation Link: https://developers.google.com/apps-script/reference/maps/maps#setAuthentication(String,String)
Quotas URL: https://console.cloud.google.com/project/_/apiui/apiview/geocoding_backend/quotas?_ga=2.141719605.643331044.1560431279-1498828710.1560431279
Documentation URL: https://developers.google.com/maps/documentation/geocoding/usage-and-billing

Daily quota limit for Maps Service exceeded, but no quota is shown as used

I have a Google Sheet with a list of places. For each place, I aim to calculate its distance from all the other places one at a time, with the aim to find the ones that are nearby to each other.
My getDistance() function below returns a float corresponding to the distance in miles between two addresses:
(I've redacted my API key and Client ID)
function getDistance(from, to) {
var key = '{my-API-key}';
var clientID = '{my-client-id}';
Maps.setAuthentication(clientID, key);
var directions = Maps.newDirectionFinder()
.setOrigin(from)
.setDestination(to)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
return parseFloat(directions.routes[0].legs[0].distance.text);
};
things I've tried:
enabled the Distance Matrix API, and the Directions API.
signed up for and enabled billing in the Google Cloud Console (as directed by a Google Support rep to no avail)
Neither of these enabled APIs show any usage in the API console, but my script stills errors with:
Service invoked too many times for one day: route
I do not see any relevant quota set for this, so what am I not understanding?

How do I monitor api request quotas or usage?

I am familiar with the Google API Console for monitoring requests quotas, but is there an endpoint or Google API Resource that I can query for poll to get current quota usage information for an enabled API, eg, Google Drive API?
AFAIK, there is no current API endpoint to get/poll the current quota usage information. As I previously mentioned in a related SO question, receiving quota related errors would help you know that you are reaching your quota for the API and that is where your error handling will take over.
To view the quota usage, access your developer console. And you are correct, it is viewable using the console. You can also see traffic ( number of request per seconds), and error ratio of the enabled API of your project.