title: .NET Gmail Api. Input is not a valid base64
body: <p>Although this seems very easy to fix, my .net application simply doesn't see this as a valid base64 string for some reason.</p>
  
  <p>I'm using GMail API to fetch the messages and on the last part where I try to retrieve the body, I hit the wall with the following error message:</p>
  
  <blockquote>
    <p>Exception thrown: 'System.FormatException' in mscorlib.dll<br>
    System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters,
    or an illegal character among the padding characters.<br>
    at System.Convert.FromBase64_ComputeResultLength(Char* inputPtr, Int32 inputLength)<br>
    at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)<br>
    at System.Convert.FromBase64String(String s)<br>
    at wForm.Form1.d__9.MoveNext() in <code>E:\Programs\Programming\wForms\wForm\wForm\Form1.cs:line 127</code></p>
  </blockquote>
  
  <p>and this is the encoded string (which decodes fine in other apps, such as the utilities-info website) : 
  <a href="https://pastebin.ca/3893521" rel="nofollow noreferrer">https://pastebin.ca/3893521</a></p>
  
  <p>My code for loading the windows form and fetching those messages:</p>
  
  <pre><code>private async void Form1_Load(object sender, EventArgs e)
  {
      txtEmail_ID.Focus();
      try
      {
          requestLabel = service.Users.Labels.List("me");
          requestMessage = service.Users.Messages.List("me");
          //requestMessage.LabelIds = "INBOX";
          //requestMessage.IncludeSpamTrash = false;
          // requestMessage.Q = "is:unread";
  
          var messageResponse = await requestMessage.ExecuteAsync();
          if (messageResponse != null &amp;&amp; messageResponse.Messages != null)
          {
              foreach (var message in messageResponse.Messages)
              {
                  var currentMessage = new messageInfo();
                  var requestNestedMessage = service.Users.Messages.Get("me", message.Id);
                  var nestedMessageResponse = await requestNestedMessage.ExecuteAsync();
  
                  if (nestedMessageResponse != null) // Check if we have nested params
                  {
                      foreach (var mParts in nestedMessageResponse.Payload.Headers) // Loop msg headers
                      {
                          if (mParts.Name == "Date")
                          {
                              currentMessage.msgDate = mParts.Value; // Get msg date
                          }
                          else if (mParts.Name == "From")
                          {
                              currentMessage.msgSender = mParts.Value; // Get msg sender
                          }
                          else if (mParts.Name == "Subject")
                          {
                              currentMessage.msgTitle = mParts.Value; // Get msg subject
                          }
                      }
                      if (nestedMessageResponse.Payload.Parts == null &amp;&amp; nestedMessageResponse.Payload.Body != null)
                      {
                          currentMessage.msgBody = nestedMessageResponse.Payload.Body.Data;
                      }
                      else
                      {
                          currentMessage.msgBody = returnNestedParts(nestedMessageResponse.Payload.Parts, "");
                      }
  
                      Console.WriteLine(currentMessage.msgSender);
                      String codedString = currentMessage.msgBody.Replace("-", "+");
                      codedString = codedString.Replace("_", "/");
  
                      byte[] data = Convert.FromBase64String(codedString);
                      currentMessage.msgBody = Encoding.UTF8.GetString(data);
                      mInfo.Add(currentMessage);
                  }
              }
          }
      } catch (Exception ex)
      {
          Console.WriteLine(ex);
      }
      //comboFolders.SelectedIndex = 0;
  }
  </code></pre>
  
  <p>The program throws the exception on <strong>every</strong> message it fetches and the "Line 127" is on "byte[] data"</p>
  
  <p>I have tried searching around in this forum for similar questions, however, none of their resolutions seem to have worked as most of them offered just to change the - and _ symbols to appropriate ones for base64url type.</p>