Example #1
0
        public GmailServiceResponse <List <Message> > GetMessages(Token token, DateTime from, DateTime to)
        {
            var gmailService = CreateGmailService(token);
            var result       = new GmailServiceResponse <List <Message> >();
            var messages     = new List <Message>();
            var request      = gmailService.Users.Messages.List("me");
            var query        = "after:{0} before:{1}";

            request.Q = string.Format(query, from.ToUnixTime(), to.ToUnixTime());
            do
            {
                try
                {
                    var response = request.Execute();
                    if (response.Messages != null)
                    {
                        messages.AddRange(response.Messages);
                        request.PageToken = response.NextPageToken;
                    }
                }
                catch (Exception ex)
                {
                    result.Error += ex.Message;
                }
            } while (!string.IsNullOrEmpty(request.PageToken));
            result.Result = messages;

            return(result);
        }
Example #2
0
        public GmailServiceResponse <Message> GetMessage(Token token, string messageId)
        {
            var gmailService = CreateGmailService(token);
            var result       = new GmailServiceResponse <Message>();
            var request      = gmailService.Users.Messages.Get("me", messageId);

            try
            {
                var response = request.Execute();
                result.Result = response;

                var messagePart = Convert.FromBase64String(response.Payload.Body.Data);
            }
            catch (Exception ex)
            {
                result.Error += ex.Message;
            }

            return(result);
        }
Example #3
0
        public async Task <GmailServiceResponse <Token> > GetToken(string code)
        {
            var result = new GmailServiceResponse <Token>();

            try
            {
                var credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId     = ClientId,
                        ClientSecret = ClientSecret,
                    },
                    Scopes = new[] { Google.Apis.Gmail.v1.GmailService.Scope.GmailModify }
                });
                var tokenResponse =
                    await credential.ExchangeCodeForTokenAsync("", code, RedirectUri, CancellationToken.None);

                var token = new Token()
                {
                    AccessToken      = tokenResponse.AccessToken,
                    ExpiresInSeconds = tokenResponse.ExpiresInSeconds,
                    IdToken          = tokenResponse.IdToken,
                    Issued           = tokenResponse.Issued,
                    IssuedUtc        = tokenResponse.IssuedUtc,
                    RefreshToken     = tokenResponse.RefreshToken,
                    Scope            = tokenResponse.Scope,
                    TokenType        = tokenResponse.TokenType,
                };
                result.Result = token;
            }
            catch (Exception ex)
            {
                result.Error = ex.Message;
            }

            return(result);
        }