【问题标题】:Flutter - Send email using Google APIFlutter - 使用 Google API 发送电子邮件
【发布时间】:2018-10-09 07:27:32
【问题描述】:

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

【问题讨论】:

  • 需要将 Content-Type 标头设置为 application/json

标签: dart flutter


【解决方案1】:

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

【讨论】:

  • @rafaelcb21 我们应该在 userId、gmail 帐户名称或 gmail id 中写什么,例如:example@gmail.com ????
  • @ArgaPK 是帐户电子邮件,例如:myemail@gmail.com
猜你喜欢
  • 2017-04-14
  • 2021-04-18
  • 1970-01-01
  • 2014-09-16
  • 2017-06-22
  • 1970-01-01
  • 2018-01-26
  • 2012-08-13
  • 1970-01-01
相关资源
最近更新 更多