Sto eseguendo uno script di app Google con la mia app C# circa una volta ogni 1,5 minuti. Lo script app sposta il contenuto tra fogli di lavoro e fogli di modifica. Utilizzo anche l'API di Drive.L'autorizzazione del servizio API di esecuzione script di Google app fallisce una volta all'ora
Il mio script funziona bene per lunghi periodi, tranne per il fatto che ottengo un errore di autorizzazione per 5 minuti ogni ora.
Ecco il mio codice che gestisce l'autorizzazione:
class Authentication
{
public static ScriptService ScriptsAuthenticateOauth(UserCredential credential)
{
try
{
ScriptService service = new ScriptService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApp",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(DateTime.Now.ToString("HH:mm") + ": An authentication error occurred: " + ex.InnerException);
return null;
}
}
public static UserCredential getCredential(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { DriveService.Scope.Drive, // view and manage your files and documents
DriveService.Scope.DriveAppdata, // view and manage its own configuration data
DriveService.Scope.DriveAppsReadonly, // view your drive apps
DriveService.Scope.DriveFile, // view and manage files created by this app
DriveService.Scope.DriveMetadataReadonly, // view metadata for files
DriveService.Scope.DriveReadonly, // view files and documents on your drive
DriveService.Scope.DriveScripts, // modify your app scripts
ScriptService.Scope.Drive,
"https://www.googleapis.com/auth/spreadsheets",
"https://spreadsheets.google.com/feeds",
"https://docs.google.com/feeds"};
return GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Google.Sheet.Sync.Auth.Store")).Result;
}
public static DriveService DriveAuthenticateOauth(UserCredential credential)
{
try
{
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApp",
});
// Console.WriteLine("Auth success");
return service;
}
catch (Exception ex)
{
// Console.WriteLine(ex.InnerException);
Console.WriteLine(DateTime.Now.ToString("HH:mm") + ": An authentication error occurred: " + ex.InnerException);
return null;
}
}
}
ottengo i miei servizi come questo:
var credential = Authentication.getCredential(CLIENT_ID, CLIENT_SECRET, Environment.UserName);
DriveService driveService = Authentication.DriveAuthenticateOauth(credential);
ScriptService scriptService = Authentication.ScriptsAuthenticateOauth(credential);
Ma verso la fine dell'ora, la chiamata Apps Script tiri il seguente errore:
Script error message: Authorization is required to perform that action.
Giusto per essere chiari, funziona in tutte le altre volte, solo non in quei 5 minuti n ascolta la fine dell'ora. Ho attivato l'API di Google Drive su entrambe le console degli sviluppatori e in Risorse> Servizi Google avanzati ... nell'editor degli script di app.
Quindi cosa sta succedendo? Questo può essere risolto?
sembra che tu stia passando un token di accesso scaduto. in tal caso, leggi su oauth2 e su come gestire i token di aggiornamento e accesso. –