我在阅读GMAIL的电子邮件时遇到问题。
这是我收到每封电子邮件的方式:
- (void)displayResultMessageWithTicket:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailMessage *)email
error:(NSError *)error
{
if (error == nil) {
NSMutableString *labelString = [[NSMutableString alloc] init];
GTLGmailMessagePart* payload=email.payload;
NSLog(@"payload =%@",payload);
for(GTLGmailMessagePart* part in payload.parts)
{
GTLGmailMessagePartBody* body = part.body;
NSLog(@"body =%@",body);
NSLog(@"data =%@",body.data);
}
self.output.text = labelString;
}
else
{
[self showAlert:@"Error" message:error.localizedDescription];
}
}
我认为身体是用base64编码的,但我不确定。
这是我得到的一个例子:
Q3J1bmNoIERhaWx5DQoNClRvZGF5J3MgVG9wIFN0b3JpZXMgLy8gSnVsIDYsIDIwMTUgUmVhZCBtb3JlIGF0IHRlY2hjcnVuY2guY29tIDwjPiBJZiB5b3UgZG8gbm90IHdhbnQgdG8gcmVjZWl2ZSB0aGlzLCB5b3UgY2FuIHVuc3Vic2NyaWJlIHdpdGggb25lIGNsaWNrIGhlcmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQjA5YWQ-Lg0KDQpUZWNoQ3J1bmNoIDQxMCBUb3duc2VuZCBTdHJlZXQsIFNhbiBGcmFuY2lzY28sIENBIDk0MTA3DQoNCsKpIDIwMTIgQU9MIEluYy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4gUHJpdmFjeSBQb2xpY3kgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQ2NiNWE-IFRlcm1zIG9mIFNlcnZpY2UgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLRDRkZjU-DQoNCklmIHlvdSBiZWxpZXZlIHRoaXMgaGFzIGJlZW4gc2VudCB0byB5b3UgaW4gZXJyb3IsIHBsZWFzZSBzYWZlbHkgdW5zdWJzY3JpYmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tL29jLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS84YzIyN2ZiNz4u
我怎么解码呢?
答案 0 :(得分:4)
消息正文是base64url编码的。你需要解码,在base64解码之前,在编码字符串中用“/”和“ - ”替换为“+”。
答案 1 :(得分:2)
补充Furhan的回答
var parts = eachItem.payload.parts
let body: AnyObject? = parts[0].valueForKey("body")
var base64DataString = body!.valueForKey("data") as! String
base64DataString = base64DataString.stringByReplacingOccurrencesOfString("_", withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)
base64DataString = base64DataString.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil)
let decodedData = NSData(base64EncodedString: base64DataString, options:NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
println(decodedString)
答案 2 :(得分:0)
有一个GTL API可以解码OOTB。 这是我的工作代码。
//... get part
let body : GTLGmailMessagePartBody = part.body
let bodyAttachmentId = body.attachmentId
let bodyData : String! = body.data
let bodySize = body.size
//Use GTLDecodeWebSafeBase64
if bodyData != nil
{
let decodedData = GTLDecodeWebSafeBase64(bodyData)
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
print("after decoded: \(decodedString)")
}
答案 3 :(得分:0)
if let bodyPart = message.payload?.parts.filter({ $0.mimeType == "text/html" }).first, let bodyDataEncoded = bodyPart.body?.data {
logger(bodyPart.body?.size)
let base64Encoded = bodyDataEncoded.replacingOccurrences(of: "_", with: "/").replacingOccurrences(of: "-", with: "+")
let data = Data(base64Encoded: base64Encoded)
logger(data?.count)
}
您可以在webview中加载数据
webView.load(data, mimeType:mimeType, textEncodingName: "", baseURL: baseUrl)
查看更多:https://developers.google.com/gmail/api/v1/reference/users/messages#resource