GMail RESTful API:-内容传输编码不一致 [英] GMail RESTful API: - content-transfer-encoding disagreement

查看:116
本文介绍了GMail RESTful API:-内容传输编码不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对RESTful API以及MIME内容都是陌生的.我正在尝试编写一个用于从Gmail邮箱中检索邮件以进行进一步处理的应用程序.

I am new to RESTful API as well as dealing with MIME content. I am trying to write an app to retrieve messages from Gmail mailbox for further processing.

我使用Google Developers API参考作为工具来熟悉API.使用以下GMail API参考

I use Google Developers API reference as a tool to get myself familiar with the API. Using the "Try it" feature in the following GMail API reference

https://developers.google.com/gmail/api/v1/reference/users/messages/get

我检索了一条只有两行文本的简单测试消息.第一行包含简单的ASCII,而第二行包含一些Unicode文本.

I retrieve a simple test message which just has 2 lines of text. The first line contains simple ASCII while the second line contains some Unicode text.

This the first line of body text in English only

This is second line, 中文睇到嗎 , could you see those Chinese

并获得以下响应

"parts": [
   {
    "partId": "0",
    "mimeType": "text/plain",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/plain; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "quoted-printable"
     }
    ],
    "body": {
     "size": 120,
     "data":  "VGhpcyB0aGUgZmlyc3QgbGluZSBvZiBib2R5IHRleHQgaW4gRW5nbGlzaCBvbmx5DQoNClRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZQ0K"
    }
   },
   {
    "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": 354,
     "data": "PGRpdiBkaXI9Imx0ciI-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnRhaG9tYSxzYW5zLXNlcmlmIj5UaGlzIHRoZSBmaXJzdCBsaW5lIG9mIGJvZHkgdGV4dCBpbiBFbmdsaXNoIG9ubHk8L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9kZWZhdWx0IiBzdHlsZT0iZm9udC1mYW1pbHk6dGFob21hLHNhbnMtc2VyaWYiPjxicj48L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9kZWZhdWx0IiBzdHlsZT0iZm9udC1mYW1pbHk6dGFob21hLHNhbnMtc2VyaWYiPlRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZTwvZGl2PjwvZGl2Pg0K"
    }
   }
  ]

如您所见,虽然将"content-transfer-encoding"指定为"quoted-printable",但实际返回的数据采用base64url编码.

As you can see, while the "content-transfer-encoding" is specified as "quoted-printable" the actual data returned is in base64url encoding.

当我使用测试脚本通过GMail RESTful API检索消息时,得到相同的64baseurl编码响应.

I get the same 64baseurl encoded response when I use my test script to retrieve the message via GMail RESTful API.

所以我的问题是,为什么标头中的内容传输编码"与实际的编码响应之间存在分歧?我是否忽略了某些内容,或者是否有其他一些参数/要求使其正确无误?

So my questions is why there is a disagreement between the "content-transfer-encoding" in header against the actual encoded response? Did I overlook something or there are some additional parameters/request to make it correct?

谢谢

基于@EricDeFriez的评论

based on the comment from @EricDeFriez

如果我base64url解码了数据"字段

If I base64url decode the 'data' field

"VGhpcyB0aGUgZmlyc3QgbGluZSBvZiBib2R5IHRleHQgaW4gRW5nbGlzaCBvbmx5DQoNClRoaXMgaXMgc2Vjb25kIGxpbmUsIOS4reaWh-edh-WIsOWXjiAsIGNvdWxkIHlvdSBzZWUgdGhvc2UgQ2hpbmVzZQ0K"

似乎结果已经是原始的utf8编码文本:

It seems the result is already the original utf8 coded text :

This the first line of body text in English only

This is second line, 中文睇到嗎 , could you see those Chinese

如果它是"quoted-printable"编码的,则base64url解码的结果应如下所示:

If it was 'quoted-printable' encoded, the result of base64url decode should look like this :

This the first line of body text in English only

This is second line, =E4=B8=AD=E6=96=87=E7=9D=87=E5=88=B0=E5=97=8E , could =
you see those Chinese

似乎JSON响应中的"quoted-printable" CTE标头不正确.无论CTE标头中指定了什么,数据字段始终使用base64url编码.

It seems that the 'quoted-printable' CTE header in the JSON response is not correct. The data field is always base64url encoded regardless what is specified in CTE header.

你知道原因吗?

推荐答案

"data"字段对于通过JSON进行本地传输不一定是安全的,因此始终采用base64url编码.一旦您对base64url解码了数据",它应该与CTE标头匹配.

The "data" field isn't necessarily safe for transport natively over JSON so it's always base64url encoded. Once you base64url decode 'data' it should match the CTE header.

这篇关于GMail RESTful API:-内容传输编码不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆