gpt4 book ai didi

dart - Flutter - 使用 Google API 发送电子邮件

转载 作者:IT王子 更新时间:2023-10-29 06:55:18 24 4
gpt4 key购买 nike

经过大量研究和修复问题,我到达了我的 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 发送电子邮件?你能帮我解决这个问题吗?

最佳答案

做了一些更改,主要是 http post 正文需要是一个带有 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());
}

关于dart - Flutter - 使用 Google API 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50079538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com