0

I'm using the Google Admin SDK in NodeJS to get a list of users. I can't seem to use the thumbnail there in a public fashion, so I'm taking their use ID and calling users.photos.get (NodeJS SDK) and in return I get web-safe Base64 photo data and mime type.

I'm trying to figure out how the hell to get my image to actually display. I'm going to the img tag and setting SRC attribute via JQuery to this inline CSS made of the format:

'data:' + photo.mimeType + ';base64,' + photo.photoData

So this comes out like: data:image/gif;base64, longAssString.

The problem is the images are always broken. I can't figure out if I'm doing something wrong, or if somehow Google is giving me some bad data or I need to do some sort of extra REGEXing on the string because it's in some 'web-safe' format?

Maybe my images are broken, but they both display fine in the Google Admin Dashboard when administrating the users (they are test users). I've tried playing with the Google Closure Library for it's decode function, but it literally decodes it into strange characters.

Ultimately I just want to get the user's photo from the Google Admin Directory so I can import it into another system!

Ivan
  • 1,093
  • 1
  • 10
  • 15

1 Answers1

4

When uploading a photo (I do this a lot), there's some additional web safe encoding that is done (in addition to a base64Encode). That encoding (on upload) is to convert a forward slash / to an underscore _ and a plus sign + to a minus sign -. In code, that's:

encodedPhoto = encodedPhoto.replace(/\//g,'_').replace(/\+/g,'-'); 

That means that when downloading, you should do the reverse:

encodedPhoto = photo.photoData.replace(/_/g,'/').replace(/-/g,'+'); 

This is documented in part here but I remember that you shouldn't replace the padding bits as per comments here.

Community
  • 1
  • 1
Peter
  • 5,501
  • 2
  • 26
  • 42
  • You are my hero! It wasn't clear in the docs if that was something that the web understood and knew how to convert back or if I had to do it myself. Even their Closure library didn't seem to really help or do what needed to be done. – Ivan Dec 14 '16 at 20:01