Ejemplo n.º 1
0
        private Google.Apis.Gmail.v1.GmailService CreateGmailService(Token token)
        {
            var tokenResponse = new TokenResponse()
            {
                AccessToken      = token.AccessToken,
                ExpiresInSeconds = token.ExpiresInSeconds,
                IdToken          = token.IdToken,
                Issued           = token.Issued,
                IssuedUtc        = token.IssuedUtc,
                RefreshToken     = token.RefreshToken,
                Scope            = token.Scope,
                TokenType        = token.TokenType,
            };

            var credential = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = ClientId,
                    ClientSecret = ClientSecret,
                },
            }),
                                                "me",
                                                tokenResponse);
            var gmailService = new Google.Apis.Gmail.v1.GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
            });

            return(gmailService);
        }
Ejemplo n.º 2
0
 ///<summary>Throws exceptions if failing to send emails or authenticate with Google.</summary>
 private static void SendEmailOAuth(BasicEmailAddress address, BasicEmailMessage message)
 {
     using GmailApi.GmailService gService = GoogleApiConnector.CreateGmailService(address);
     try {
         GmailApi.Data.Message gmailMessage = CreateGmailMsg(address, message);
         gService.Users.Messages.Send(gmailMessage, address.EmailUsername).Execute();
     }
     catch (GoogleApiException gae) {
         gae.DoNothing();
         //This will bubble up to the UI level and be caught in a copypaste box.
         throw new Exception("Unable to authenticate with Google: " + gae.Message);
     }
     catch (Exception ex) {
         //This will bubble up to the UI level and be caught in a copypaste box.
         throw new Exception($"Error sending email with OAuth authorization: {ex.Message}");
     }
 }