经过大量研究和修复问题后,我到达了我github的以下位置。但我不知道我是否正在安装json。出现以下错误:
{
error:
{
errors: [
{
domain: global,
reason: parseError,
message: This API does not support parsing form-encoded input.
}
],
code: 400,
message: This API does not support parsing form-encoded input.
}
}
我正在设置帖子如下,有关详细信息,该项目位于我的github
// scope for send email
GoogleSignIn googleSignIn = new GoogleSignIn(
scopes: <String>[
'https://www.googleapis.com/auth/gmail.send'
],
);
await googleSignIn.signIn().then((data) {
testingEmail(data.email, data.authHeaders);
});
// userId is the email
Future<Null> testingEmail(userId, header) async {
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: await header,
body: {
'from': userId,
'to': userId,
'subject': 'testing send email',
'text': 'worked!!!'
}
);
}
我做错了什么,无法通过Google API发送电子邮件?你能帮我解决这个问题吗?
答案 0 :(得分:2)
进行了一些更改,主要的一点是http帖子正文需要是raw
密钥的json,其内容在base64
中。此文本已转换为base64必须是一个MIMEText,所以具体格式如下。
要将html更改为文字,只需将Content-Type: text/html
从字符串更改为Content-Type: text/plain
以下是代码的剪辑。完整代码位于github
await googleSignIn.signIn().then((data) {
data.authHeaders.then((result) {
var header = {'Authorization': result['Authorization'], 'X-Goog-AuthUser': result['X-Goog-AuthUser']};
testingEmail(data.email, header);
});
});
Future<Null> testingEmail(String userId, Map header) async {
header['Accept'] = 'application/json';
header['Content-type'] = 'application/json';
var from = userId;
var to = userId;
var subject = 'test send email';
//var message = 'worked!!!';
var message = "Hi<br/>Html Email";
var content = '''
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: ${to}
from: ${from}
subject: ${subject}
${message}''';
var bytes = utf8.encode(content);
var base64 = base64Encode(bytes);
var body = json.encode({'raw': base64});
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: header,
body: body
);
if (response.statusCode != 200) {
setState(() {
print('error: ' + response.statusCode.toString());
});
return;
}
final Map<String, dynamic> data = json.decode(response.body);
print('ok: ' + response.statusCode.toString());
}