When I use Python to generate a base64 string that will be used in the raw key { 'raw': value } GMAIL API, sending the email occurs perfectly.
But when I use Dart to generate the same base64 string, the string is not the same as python and because of that I can not send the email because the GMAIL API tells me message: Invalid value for ByteString
The string that will be converted to base64 is:
var message = '''
Test'''Python code:
import base64
e = base64.urlsafe_b64encode('''
Test''')print(e)
result:
PGh0bWw-PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiLz48aGVhZD48L2hlYWQ-PGJvZHk-VGVzdDwvYm9keT48L2h0bWw-
Dart code:
import 'dart:convert';
var _bytes = utf8.encode('''
Test''');var _base64 = base64Encode(_bytes);
print(_base64);
result:
PGh0bWw+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiLz48aGVhZD48L2hlYWQ+PGJvZHk+VGVzdDwvYm9keT48L2h0bWw+
Note that the only difference is the + sign in the base64 string of the Dart, and the - sign in the base64 string of Python
How can I generate the same base64 python code, so I can send the email in the GMAIL API
解决方案
Dart has a URL safe version of Base 64 encode, like Python.
Change
var _base64 = base64Encode(_bytes);
to
var _base64 = base64UrlEncode(_bytes);