python字符不等于_Dart-Base64字符串不等于python

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值