2

I am trying to update photo using Google apps engine.I have imageurl i convert it into byte array and then encode it using base64.i got encoded string,now i m trying to update photodata using directory API Reference

https://developers.google.com/admin-sdk/directory/v1/reference/users/photos#resource

after update i got error invalid byteString.I face this problem from yesterday.So Let me know where i did wrong? Below is my code.

import com.google.appengine.repackaged.org.apache.commons.codec.binary.Base64;

above class used for Base64.

 URL url = new URL(myImageUrl);

            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            InputStream is = null;
            try {
              is = url.openStream ();
              byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
              int n;

              while ( (n = is.read(byteChunk)) > 0 ) {
                bais.write(byteChunk, 0, n);
              }
              System.out.println(byteChunk);
              byte[] encoded = Base64.encodeBase64(byteChunk);
              String ecodedString = new String(encoded, "UTF-8");
              ecodedString = ecodedString.replace("/", "_");
              ecodedString = ecodedString.replace("+", "-");
              ecodedString = ecodedString.replace("=", "*");
              System.out.println(ecodedString);
Ravi Chothe
  • 180
  • 5
  • 17

1 Answers1

4

Padding could be the problem, try not replacing "=" with "*". See also: Converting string to web-safe Base64 format

p.s. repackaged libs are discouraged in app engine; you may use DatatypeConverter.printBase64Binary() instead.

Community
  • 1
  • 1
OmegaDirective
  • 366
  • 2
  • 6