如何解码GMail API返回的message.parts?

时间:2016-04-06 19:15:09

标签: c# json base64

我使用Gmail API从服务器获取一些消息。它返回一个长json数组。这是相关部分:

"parts": [
   {
    "partId": "0",
    "mimeType": "text/plain",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/plain; charset=UTF-8"
     }
    ],
    "body": {
     "size": 91,
     "data": "dGVzdA0KLS0NCkRhd2lkIChSZXluZXZhbikgU2VyYWZpbg0KKmh0dHA6Ly93d3cucmV5bmV2YW4uY29tLyA8aHR0cDovL3d3dy5yZXluZXZhbi5jb20vPioNCg=="
    }
   },
   {
    "partId": "1",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/html; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "quoted-printable"
     }
    ],
    "body": {
     "size": 391,
     "data": "PGRpdiBkaXI9Imx0ciI-dGVzdDxiciBjbGVhcj0iYWxsIj48ZGl2PjxkaXYgY2xhc3M9ImdtYWlsX3NpZ25hdHVyZSI-PGRpdiBkaXI9Imx0ciI-PGRpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj4tLTwvZm9udD48L2Rpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj5EYXdpZCAoUmV5bmV2YW4pIFNlcmFmaW48L2ZvbnQ-PGRpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj48Yj48aT48YSBocmVmPSJodHRwOi8vd3d3LnJleW5ldmFuLmNvbS8iIHRhcmdldD0iX2JsYW5rIj5odHRwOi8vd3d3LnJleW5ldmFuLmNvbS88L2E-PC9pPjwvYj48L2ZvbnQ-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-DQo8L2Rpdj4NCg=="
    }
   }
  ]

我可以使用此代码解析第一个问题:

foreach (Match b in body)
{
    var raw = b.Groups[1].Value;
    var bytes = Convert.FromBase64String(raw);
    var s = Encoding.UTF8.GetString(bytes);
    bodyDecoded += s;
}

但第二个看起来与第一个不同,此代码抛出FormatException

我该怎么办?这仍然是base64吗?我应该删除破折号吗?

编辑:这是另一个字符串:R3JlZywgdGhlIE5pa2UgQWlyIE1heCAxIFYgU1AgJ1BhdGNoJyBoYXMgZHJvcHBlZCDigJMgaGVyZSdzIHlvdXIgcHVyY2hhc2UNCmxpbmsuDQo8aHR0cHM6Ly9sYXVuY2hlcy5lbmRjbG90aGluZy5jb20vPg0KTmlrZSBBaXIgTWF4IDEgViBTUCAnUGF0Y2gnDQoNClNhbmQNCltpbWFnZTogUHJvZHVjdCBpbWFnZV0NCg0KRFJPUFMgSU4NCg0KKjAwKiBkYXlzIDogKjAwKiBocnMgOiAqMDAqIG1pbnMgOiAqMDAqIHNlY3MNCg0KUGxlYXNlIGNsaWNrIHRoZSBsaW5rIGJlbG93IHRvIGpvaW4gdGhlIHF1ZXVlIGZvciBwdXJjaGFzaW5nIHRoaXMgcHJvZHVjdC4NClJlbWVtYmVyIHRvIGh1cnJ5IC0gc3RvY2sgaXMgYWxsb2NhdGVkIG9uIGEgZmlyc3QgY29tZSBmaXJzdCBzZXJ2ZSBiYXNpcy4NCkNsaWNraW5nIHRoaXMgbGluayBkb2VzIG5vdCBndWFyYW50ZWUgcHVyY2hhc2UuDQpDbGljayBIZXJlDQo8aHR0cHM6Ly9sYXVuY2hlcy5lbmRjbG90aGluZy5jb20vIy9jaGVja291dC9rMnZ4a243bjYyMXk3NjF5dHA0dDJjbXB5N2ZjZTYwcnlqYzd4OWp0L2xvZ2luPg0KRU5ELg0KVW5pdCBDIE1lcmxpbiBXYXkNCk5ld2Nhc3RsZSBVcG9uIFR5bmUNCk5FMjcgMFFHDQorNDQgKDApMzMzIDMyMyA3NzI4DQppbmZvQGVuZGNsb3RoaW5nLmNvbQ0KW2ltYWdlOiBGYWNlYm9va10gW2ltYWdlOiBHb29nbGUrXSBbaW1hZ2U6IEluc3RhZ3JhbV0gW2ltYWdlOiBUd2l0dGVyXQ0K

由eser编辑

这就是我的答案中的代码产生(过滤)

XXXXXX, the XXXXXX 1 V SP 'Patch' has dropped – here's your purchase
link.
<https://launches.XXXXXXXX.com/>
XXXXXX 1 V SP 'Patch'

Sand
[image: Product image]

DROPS IN

*00* days : *00* hrs : *00* mins : *00* secs

Please click the link below to join the queue for purchasing this product.
Remember to hurry - stock is allocated on a first come first serve basis.
Clicking this link does not guarantee purchase.
Click Here
<https://launches.XXXXXXX.com/#/checkout/XXXX/login>
END.
XXXXXXXXXXXXXXXXXXXXXx
XXXXXXXXXXXXXXXXXXXX
NE27 0QG
+44 (0)XXX XXX XXXX
info@XXXXXXX.com
[image: Facebook] [image: Google+] [image: Instagram] [image: Twitter]

2 个答案:

答案 0 :(得分:2)

-不是有效的base64字符...我不知道使用该字符的原因,只是将-替换为+并且您是&#39;我会得到正确的HTML ..

string data = "PGRpdiBkaXI9Imx0ciI-dGVzdDxiciBjbGVhcj0iYWxsIj48ZGl2PjxkaXYgY2xhc3M9ImdtYWlsX3NpZ25hdHVyZSI-PGRpdiBkaXI9Imx0ciI-PGRpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj4tLTwvZm9udD48L2Rpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj5EYXdpZCAoUmV5bmV2YW4pIFNlcmFmaW48L2ZvbnQ-PGRpdj48Zm9udCBmYWNlPSJjb21pYyBzYW5zIG1zLCBzYW5zLXNlcmlmIj48Yj48aT48YSBocmVmPSJodHRwOi8vd3d3LnJleW5ldmFuLmNvbS8iIHRhcmdldD0iX2JsYW5rIj5odHRwOi8vd3d3LnJleW5ldmFuLmNvbS88L2E-PC9pPjwvYj48L2ZvbnQ-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-DQo8L2Rpdj4NCg==";

var base64 = Convert.FromBase64String(data.Replace("-", "+"));
var str = Encoding.UTF8.GetString(base64);

结果:

<div dir="ltr">test<br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><font face="comic sans ms, sans-serif">--</font></div><font face="comic sans ms, sans-serif">Dawid (Reynevan) Serafin</font><div><font face="comic sans ms, sans-serif"><b><i><a href="http://www.reynevan.com/" target="_blank">http://www.reynevan.com/</a></i></b></font></div></div></div></div> </div>

答案 1 :(得分:0)

Google Gmail API使用Base64编码的变体,可以安全地在URL中使用。该方案将'-'字符替换为'+'字符,将'/'字符替换为'_'字符,并删除填充在字符串末尾的所有尾随'='字符。

有关详细信息和转换代码,请参见以下问题: How to achieve Base64 URL safe encoding in C#?