dabirbswerenoisyaround
dabirbswerenoisyaround
CC#
Created by dabirbswerenoisyaround on 2/16/2024 in #help
How can I run a Google API inside a Docker Container?
I'm making a worker service that interacts with the Gmail API. Currently, I use OAuth 2.0 Client for the credentials:
public static class GmailApiHelper
{
public static GmailService GetGmailService(string credentialsPath, string accessTokenPath)
{
using var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read);

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailModify },
"user",
CancellationToken.None,
new FileDataStore(accessTokenPath, true)).Result;

return new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Gmail API Sample",
});
}
}
public static class GmailApiHelper
{
public static GmailService GetGmailService(string credentialsPath, string accessTokenPath)
{
using var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read);

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailModify },
"user",
CancellationToken.None,
new FileDataStore(accessTokenPath, true)).Result;

return new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Gmail API Sample",
});
}
}
It works fine until I run it on a Docker Container:
2024-02-16 19:08:21 [11:08:21 ERR] [Microsoft.Extensions.Hosting.Internal.Host] - BackgroundService failed
2024-02-16 19:08:21 System.InvalidOperationException: At least one client secrets (Installed or Web) should be set
2024-02-16 19:08:21 at Google.Apis.Auth.OAuth2.GoogleClientSecrets.get_Secrets()
2024-02-16 19:08:21 [11:08:21 ERR] [Microsoft.Extensions.Hosting.Internal.Host] - BackgroundService failed
2024-02-16 19:08:21 System.InvalidOperationException: At least one client secrets (Installed or Web) should be set
2024-02-16 19:08:21 at Google.Apis.Auth.OAuth2.GoogleClientSecrets.get_Secrets()
I've search through this on the internet and it wants me to use a Service Account. But I couldn't get it to work. I also tried using a normal API key but it says that it has limited access. I hope someone can help me with this. 😇
630 replies