0

I'm building a web service that's sends user information to Google. All is working fine, till I do multiple requests. It fails after 10-20 successful requests.

As login method do I use the Google Service Account.

This is code u use to test the service request.

while (i < 1000)
{
    Console.WriteLine("================== Start run "+ i + " =======================");

    String serviceAccountEmail = "#####@developer.gserviceaccount.com";
    X509Certificate2 certificate = new X509Certificate2(@"pathtocert", "notasecret", X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes = new[] { 
                DirectoryService.Scope.AdminDirectoryUser.ToString(), DirectoryService.Scope.AdminDirectoryUserReadonly.ToString(),
                DirectoryService.Scope.AdminDirectoryGroup.ToString(), DirectoryService.Scope.AdminDirectoryGroupReadonly.ToString(),
                DirectoryService.Scope.AdminDirectoryOrgunit.ToString()
            },
            User = "admin@gmail.com"
        }.FromCertificate(certificate)
    );
    DirectoryService service = new DirectoryService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Bla" });

    UsersResource.GetRequest req = service.Users.Get("test@test.com");
    req.Alt = UsersResource.GetRequest.AltEnum.Json;
    Google.Apis.Admin.Directory.directory_v1.Data.User test = req.Execute();

    Console.WriteLine("ID: " + test.Id);
    Console.WriteLine("Name: " + test.Name.FullName);
}

The code runs fine if I place the creation of the service outside of the while loop. When its inside of the loop it will fail after some loops. the are given by the service is:

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Google.Apis.dll Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

Can someone give me a hand? I don't get the error and doent understand why the code sometimes works and sometimes fails.

Update Done some test runs again and found out that the BaseClientService.Initializer is the part the gives the error. Also I used fiddler to decrypted the service response. This part explains the json parse error, because the service returns a webpage. The page contained the following message:

403. That�s an error. We're sorry, but you do not have access to this page. That�s all we know.

So after some time Google blocks my request. My question is why, and why do the send a webpage if the service wants a JSON response?

Update After the tip from Alex I looked into the max request for the api, but the limit of the api is by default 15 request/second. The application never fires this many requests. To be sure I set the limit to 50 request/second, but this isn't changing anything.

Update After contacting Google looked again into the max request. There is a max User requests of 10 request/second. So to make sure the app did not overuse this quota I build in a timeout after every request of 5 sec, but I still get the same error after multiple loops(this time after 17 runs). As far as my knowledge goes, this can't be a quato over use.

What to try next?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You're being recognized as a DoS attacker and blocked (requests rate is capped) – Alex Mar 10 '14 at 13:23
  • But why do i get recognized as DoS attacker? My request are valid and my credentials are correct. In my opnion its not strange to do bulk request to a service. – Peter-Paul Frijlink Mar 10 '14 at 13:31
  • Too many requests per time frame: you probably can circumvent this if you `Sleep` for a couple seconds between each iteration of the while – Alex Mar 10 '14 at 13:34
  • This can be a soulution for the test application. But the real project is a webservice how gets called by external code, so i don't have any affect on how many request are done. – Peter-Paul Frijlink Mar 10 '14 at 13:39
  • Maybe the limits can be increased/removed by paying but I have no experience with G.APIs – Alex Mar 10 '14 at 13:43

0 Answers0