title: Converting string to web-safe Base64 format
body: <p>I am testing how to update user picture using the Admin SDK Directory Service with Google Apps Scripts with the following function:</p>
  
  <pre><code>function updatePhoto(){
    var fileId = 'XXXXXXXXXXXXXXXXXXX';
    var b = DocsList.getFileById(fileId).getBlob();
    var encoded = Utilities.base64Encode(b.getBytes());
  
    encoded = encoded.replace(/\//g,'_').replace(/\+/g,'-').replace(/\=/g,'*');
    AdminDirectory.Users.Photos.update({
      "photoData": encoded },'harry.potter@abc.edu.hk');
  }
  </code></pre>
  
  <p>However, it doesn't always work. Whenever there is padding in the base64 encoded string, it fails. Referring to Google's document (<a href="https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update" rel="nofollow">https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update</a>), I am a bit confused with the descriptions. It says:</p>
  
  <ol>
  <li>The equals sign (=) character is replaced with the asterisk (*).</li>
  <li>For padding, the period (.) character is used instead of the
  RFC-4648 baseURL definition which uses the equals sign (=) for
  padding. This is done to simplify URL-parsing.</li>
  </ol>
  
  <p>What should be actually done? (=) is used for padding in Base64. So, should I use (*) or (.)? I did try to replace (=) with (.) but no luck. </p>
  
  <p>Can anyone help?</p>
  
  <hr>
  
  <p>It is so strange. It works when i do not replace (=).</p>
  
  <pre><code>function updatePhoto(){
    var fileId = 'XXXXXXXXXXXXXXXXXXX';
    var b = DocsList.getFileById(fileId).getBlob();
    var encoded = Utilities.base64Encode(b.getBytes());
  
    encoded = encoded.replace(/\//g,'_').replace(/\+/g,'-');
    AdminDirectory.Users.Photos.update({
      "photoData": encoded },'harry.potter@abc.edu.hk');
  </code></pre>
  
  <p>}</p>