Dart-Base64字符串不等于python [英] Dart - Base64 string is not equal to python
问题描述
当我使用Python生成 base64
字符串时,将在 raw键{'raw':value} $ c中使用该字符串$ c>
GMAIL API
,发送电子邮件非常方便。
但是当我使用Dart生成相同的 base64
字符串,该字符串与python不同,因此我无法发送电子邮件,因为GMAIL API告诉我 message:无效的值ByteString
将转换为base64的字符串为:
var message ='''< html><元http-equiv = content-type content = text / html; charset = utf-8 />< head>< ; / head>< body> Test< / body>< / html>'''
Python代码:
import base64
e = base64.urlsafe_b64encode('''< html>< ; meta http-equiv = content-type content = text / html; charset = utf-8 />< head>< / head>< body> Test< / body>< / html> ''')
pri nt(e)
结果:
PGh0bWw-PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiLz48aGVhZD48L2hlYWQ-PGJvZHk-VGVzdDwvYm9keT48L2h0bWw -
达特代码:
import'dart:convert';
var _bytes = utf8.encode('''< html><元http-equiv = content-type content = text / html; charset = utf-8 />< head> < / head< body> Test< / body>< / html>''');
var _base64 = base64Encode(_bytes);
print(_base64);
结果:
PGh0bWw + PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiLz48aGVhZD48L2hlYWQ + PGJvZHk + VGVzdDwvYm9keT48L2h0bWw +
注意,唯一的区别是 +
登录Dart的base64字符串,并-
登录Python的base64字符串
如何我可以生成相同的base64 python代码,因此可以在GMAIL API中发送电子邮件
Dart具有网址安全版本
更改
var _base64 = base64Encode( _bytes);
到
var _base64 = base64UrlEncode(_bytes);
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 = '''<html><meta http-equiv="content-type" content="text/html; charset=utf-8"/><head></head><body>Test</body></html>'''
Python code:
import base64
e = base64.urlsafe_b64encode('''<html><meta http-equiv="content-type" content="text/html; charset=utf-8"/><head></head><body>Test</body></html>''')
print(e)
result:
PGh0bWw-PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiLz48aGVhZD48L2hlYWQ-PGJvZHk-VGVzdDwvYm9keT48L2h0bWw-
Dart code:
import 'dart:convert';
var _bytes = utf8.encode('''<html><meta http-equiv="content-type" content="text/html; charset=utf-8"/><head></head><body>Test</body></html>''');
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);
这篇关于Dart-Base64字符串不等于python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!