Dart-Base64字符串不等于python [英] Dart - Base64 string is not equal to python

查看:99
本文介绍了Dart-Base64字符串不等于python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Python生成 base64 字符串时,将在 raw键{'raw':value} 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆