经过大量研究和修复问题,我到达了我的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.
}
}
Run Code Online (Sandbox Code Playgroud)
我正在设置帖子如下,更多细节项目在我的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!!!'
}
);
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么,无法通过 Google API 发送电子邮件?你能帮我解决这个问题吗?
做了一些改动,主要是http post body需要是一个json,rawkey和内容在里面,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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5023 次 |
| 最近记录: |