I created a program that uploads videos to YouTube using the YouTube Data API.
My program's flow is:
- Login google account in google page using the URL
https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&include_granted_scopes=true&client_id={YouTubeAppId}&redirect_uri={RedirectURL}&response_type=code&scope={Uri.EscapeDataString(https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/userinfo.profile)}
- Exchange code to access token:
POST
https://oauth2.googleapis.com/token
- Get user's channel's IDs:
GET
https://youtube.googleapis.com/youtube/v3/channels?part=snippet,statistics&mine=true&access_token={AccessToken}
. Then I select the video and split it to 1Mb chunks. - Get upload URL:
POST
https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails&access_token={token}
with model - Send chunks of video using URL from
Location
header in previous step.
Also I've got 10min timer which refreshes access token.
foreach (SocialLogin item in logins.Where(x => (x.UpdatedOn.AddSeconds(x.ExpirationSeconds) - DateTime.Now).TotalSeconds < 600))
{
SocialLogin youModel = await _youtubeService.RefreshTokenAsync(item);
youModel.UpdatedOn = DateTimeOffset.Now;
youModel.ExpirationSeconds = 3600;
await _loginRepository.UpdateAsync(youModel);
}
This function gets all YouTube logins from the database and refreshes tokens for those logins that expire less than 10 minutes using POST
on the URL https://oauth2.googleapis.com/token
.
I uploaded couple of 15mb videos and it works well, but after it Queries per day quota counted about 2500 queries. I added breakpoints for all methods which make API calls and counted them all. There was about 50 requests at all. After couple of hours I sent another 2 30mb videos and this quota had about 8000 queries. How is this quota calculated? And what does it calculate?
Now it's 0 but yesterday it was 8120.