Home > Enterprise >  Google Sheet Api - Error when sending the request in ASP .NET
Google Sheet Api - Error when sending the request in ASP .NET

Time:01-31

I am using Google Sheets Api to read information from a sheet. Locally on Visual Studio, the code works properly, I am able to get all the information.

However when I implement my website on IIS I get the following error:

Error al enviar la solicitud (error occuring while sending the request)

The exception message is not very specific about what is happening and where.

I don't know if the IIS executed by Visual Studio has a different configuration and this is why is working locally.

This is my code

try {
         string path = Server.MapPath("Updating");

                ServiceAccountCredential credential;
                string[] Scopes = { SheetsService.Scope.Spreadsheets };
                string serviceAccountEmail = "account.com";
                string jsonfile = Path.Combine(path, "cred.json");

                using (Stream stream = new FileStream(jsonfile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    credential = (ServiceAccountCredential)
                        GoogleCredential.FromStream(stream).UnderlyingCredential;

                    var initializer = new ServiceAccountCredential.Initializer(credential.Id)
                    {
                        User = serviceAccountEmail,
                        Key = credential.Key,
                        Scopes = Scopes
                    };
                    credential = new ServiceAccountCredential(initializer);
                }
                var service = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });
                String spreadsheetId = "1zkqBR9svQInKwM9hqPzrzhOFQB....";

                string range = "Info!A:D";
                string firmasRange = "Info!A:D";

                SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);
                var response = request.Execute(); //It seems that the error happens here

     
                IList<IList<Object>> values = response.Values;

                sincronizeSheet(values);
}

This is the stacktrace

CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) en Google.Apis.Auth.OAuth2.ServiceAccountCredential.d__33.MoveNext() --- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción --- en System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) en Google.Apis.Auth.OAuth2.TokenRefreshManager.d__12.MoveNext() --- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción --- en System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) en Google.Apis.Auth.OAuth2.TokenRefreshManager.<g__LogException|10_0>d.MoveNext() --- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción --- en System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() en Google.Apis.Requests.ClientServiceRequest`1.Execute() en Xerox_Mailroom.CoordinarCelular.ButtonSincronizar_Click(Object sender, EventArgs e)

CodePudding user response:

For someone who is getting the same error the problem was pretty obvious, the connection with google api is made by the server, and it doesn't have internet access, so it never could validate the token.

  • Related