title: gmail api returning null for a specific history id. History id received from Cloud Pub/Sub push subscription
body: <p>From Cloud pub/sub push service i got a history id. Using that history id i am trying to read the recent mail's but It returns null.</p>
  
  <p>I have configured cloud pub/sub push subscription and add a watch to "Unread" label.</p>
  
  <p>Scenario 1:
  I have received a push notification. From that push notification i have taken history id to get the recent messages. it's returning me null value.</p>
  
  <p>Scenario 2:
  I have logged into that configured mail id and then the message loaded in inbox. After that if i try to read i am getting the history list.</p>
  
  <pre><code>    static string[] Scopes = { GmailService.Scope.MailGoogleCom };
      static void Main(string[] args)
      {
       string UserId = "####.gmail.com";
       UserCredential credential;
       using (var stream =
                      new FileStream("client_secret_#####.json", FileMode.Open, FileAccess.Read))
                  {             
                      string credPath = "token.json";
                      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                          GoogleClientSecrets.Load(stream).Secrets,
                          Scopes,
                          UserId,
                          CancellationToken.None,
                          new FileDataStore(credPath, true)).Result;
                      Console.WriteLine("Credential file saved to: " + credPath);
                  }
  
       var service = new GmailService(new BaseClientService.Initializer()
                  {
                      HttpClientInitializer = credential,
                      ApplicationName = ApplicationName,
                  });
  
       List&lt;History&gt; result = new List&lt;History&gt;();
                  UsersResource.HistoryResource.ListRequest request = service.Users.History.List(UserId);
      //history id received from cloud pub/sub push subscription.
                  request.StartHistoryId = Convert.ToUInt64("269871");
  
      do
                  {
                      try
                      {
                          ListHistoryResponse response = request.Execute();
                          if (response.History != null)
                          {
                              result.AddRange(response.History);
                          }
                          request.PageToken = response.NextPageToken;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("An error occurred: " + e.Message);
                      }
                  } while (!String.IsNullOrEmpty(request.PageToken));
  
  
        foreach (var vHistory in result)
                  {
                      foreach (var vMsg in vHistory.Messages)
                      {
                          string date = string.Empty;
                          string from = string.Empty;
                          string subject = string.Empty;
                          string body = string.Empty;
  
                          var emailInfoRequest = service.Users.Messages.Get(UserId, vMsg.Id);
                          var emailInfoResponse = emailInfoRequest.Execute();
                          if(emailInfoResponse!= null)
                          {
                              foreach (var mParts in emailInfoResponse.Payload.Headers)
                              {
                                  if (mParts.Name == "Date")
                                  {
                                      date = mParts.Value;
                                  }
                                  else if (mParts.Name == "From")
                                  {
                                      from = mParts.Value;
                                  }
                                  else if (mParts.Name == "Subject")
                                  {
                                      subject = mParts.Value;
                                  }
                                  if (date != "" &amp;&amp; from != "")
                                  {
                                      if (emailInfoResponse.Payload.Parts != null)
                                      {
                                          foreach (MessagePart p in emailInfoResponse.Payload.Parts)
                                          {
                                              if (p.MimeType == "text/html")
                                              {
                                                  byte[] data = FromBase64ForUrlString(p.Body.Data);
                                                  body = Encoding.UTF8.GetString(data);
                                              }
                                              else if(p.Filename!=null &amp;&amp; p.Filename.Length&gt;0)
                                              {
                                                  string attId = p.Body.AttachmentId;
                                                  string outputDir = @"D:\#####\";
                                                  MessagePartBody attachPart = service.Users.Messages.Attachments.Get(UserId, vMsg.Id, attId).Execute();
                                                  String attachData = attachPart.Data.Replace('-', '+');
                                                  attachData = attachData.Replace('_', '/');
                                                  byte[] data = Convert.FromBase64String(attachData);
                                                  File.WriteAllBytes(Path.Combine(outputDir, p.Filename), data);
                                              }
                                          }
                                      }
                                      else
                                      {
                                          byte[] data = FromBase64ForUrlString(emailInfoResponse.Payload.Body.Data);                                   
                                          body = Encoding.UTF8.GetString(data);
                                      }
                                  }
                              }
                          }
                      }
                  }
   public static byte[] FromBase64ForUrlString(string base64ForUrlInput)
      {
          int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
          StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
          result.Append(String.Empty.PadRight(padChars, '='));
          result.Replace('-', '+');
          result.Replace('_', '/');
          return Convert.FromBase64String(result.ToString());
      }
  
      }
  </code></pre>
  
  <p>Please let me know how to read the full message using history id. when i receive push notification.</p>