1

I am getting email attachment(images) from Gmail API which returns base64url encoded string. But actual encoded is different from this gmail api("/" is replaced by "_" and "+" is replaced by "-" etc). I want to get original base64 encoded string instead of base64 url encoding format. Shall i replace that two symbols("/","+") from gmail api response or can anyone please help me to achieve this? i am trying this using WSO2 EI 6.3.0.

 <call>
                    <endpoint>
                        <http method="get" uri-template="{+uri.var.gmail.apiUrl}/{+uri.var.gmail.apiVersion}/users/{+uri.var.gmail.userId}/messages/{+uri.var.id}/attachments/{+uri.var.attachmentId}"/>
                    </endpoint>
                </call>
 <property description="emailAttachment" expression="//data/text()" name="emailAttachment" scope="default" type="STRING"/>

for eg,

from Gmail API : after that call, it returns following value in "data" key.

_9j_4AAQSkZJRgABAQAASABIAAD_4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAAqACAAQAAAABAAAPwKADAAQAAAABAAAL0AAAAAD_....

original Base64 Encoded value:

/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAAqACAAQAAAABAAAPwKADAAQAAAABAAAL0AAAAAD/....

Justin
  • 855
  • 2
  • 11
  • 30
  • Possible duplicate of [Base64 decoding of MIME email not working (GMail API)](https://stackoverflow.com/questions/24812139/base64-decoding-of-mime-email-not-working-gmail-api) – Mike Doe Oct 09 '19 at 06:32
  • No, It's not. I am expecting Base64 Encoded value. I need to convert base64url encoded value which comes from gmail api to base64 encoded value. – Justin Oct 09 '19 at 07:00
  • And did you actually read it? – Mike Doe Oct 09 '19 at 07:09
  • In which language do you want to perform the conversion? – ziganotschka Oct 09 '19 at 07:27
  • Hi @ziganotschka, I am performing this operation in WSO2 EI. for that i have include the way i get base64url Encoded string from gmail for attachment. so i need to complete this in WSO2 EI. i can use script also. for eg. ` ` – Justin Oct 09 '19 at 07:31

1 Answers1

0

In Javascript the easiest would be to use the replace() method:

newData=data.replace(/-/g, '+').replace(/_/g, '/')

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Hi @ziganotschka, if i tried the above in my script mediator, it'll through exception like `ERROR - ScriptMediator The script engine returned an error executing the inlined js script function mediate com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: class java.lang.String replace(char,char) class java.lang.String` in addition to that, **if i replace that two symbol, will it work for all cases?** – Justin Oct 09 '19 at 08:33
  • I am not sure what your variable `data` is. `replace()` is a method that expects a string as parameter. I do not see any reason why it should not work in all cases. – ziganotschka Oct 09 '19 at 09:23
  • Hi @ziganotschka, i am also checking the same. but above mentioned replaceAll() is works fine. **emailAttachment** in above is base64url encoded string. what i need to do is i want to get base64 encoded string from base64 url encoded value?if i just replace that two symbol which will work for all cases(ie.conversion from base64url to base64 encoded)? – Justin Oct 09 '19 at 09:55
  • 1
    Yes. `+` and `/` for `toBase64` versus `-` and `_` for `toBase64URL` are the only differences. – ziganotschka Oct 09 '19 at 10:36
  • Hi @ziganotschka, I have done for converting from base64url encoded into base64 encoding format by replacing both symbol which works fine. Thanks for your support – Justin Oct 15 '19 at 04:07
  • @Justin, I am glad it helped! If you found my answer useful, I would appreciate if you could mark it as accepted. – ziganotschka Oct 15 '19 at 08:49