- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在关注 Google Gmail API我的 iOS 项目中的文档,我需要使用 userId、messageId 和 id 从邮件下载附件 Try out from Google . 其中 userId 是用户的电子邮件地址, id 是 mailId ( Try it.Enter only your EmailId ) messageId 是我们通过使用带有附件的上述 id 之一获得的。
到目前为止,这是我拥有的代码:
- (void)fetchLabels {
GTLQueryGmail *query = [GTLQueryGmail queryForUsersThreadsList];
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
- (void)displayResultWithTicket:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailListLabelsResponse *)labelsResponse
error:(NSError *)error {
NSDictionary *allIDsDictionaryValue=[labelsResponse.JSON valueForKey:@"threads"];
NSMutableArray *allIDs=[NSMutableArray new];
for (int i=0; i<[allIDsDictionaryValue count]; i++) {
[allIDs addObject:[allIDsDictionaryValue valueForKey:@"id"]];
}
NSString *mailID=[[allIDs objectAtIndex:0]objectAtIndex:0];
NSLog(@"%@",mailID);//Ex: 14e8af86776e595b
//As of now I am using the first id only because it has an attachment.
// Getting messageId from an id of a mail.
GTLQueryGmail *query1 = [GTLQueryGmail queryForUsersMessagesGet];
query1.identifier=mailID;
[self.service executeQuery:query1
delegate:self
didFinishSelector:@selector(displayResultWithTicketOfAttachment:finishedWithObject:error:)];
}
- (void)displayResultWithTicketOfAttachment:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailListLabelsResponse *)labelsResponse
error:(NSError *)error {
NSDictionary *response1=labelsResponse.JSON;
NSDictionary *response2=[[[response1 valueForKey:@"payload"] valueForKey:@"parts"]objectAtIndex:1];
if ([[response2 valueForKey:@"mimeType"] isEqualToString:@"application/vnd.ms-excel"]) {
attachmentId=[[response2 valueForKey:@"body"]valueForKey:@"attachmentId"];
NSLog(@"%@",attachmentId); //Ex: ANGjdJ_Uw2o7G2Q16jfC4JSTwD2UIO6LuqDIJZvXFCft0q5ALYuiYfM2gPWG0dcty2n6ZkjnVWekIb_3e2_0TqSpctWNAABsfaSF2x2ktf9uHu63e3eIot_GFA9xgppmaRDyaJEL1V-gIvjfPRxgINK8xM0OuuwnVz2xzcCFckkwpK2XwzZG_QPGPvC2Be2bGKNJI8Ds3hGtqfHYeSWZjOjbsjBuxbUFjf1Mlp0TLol9LLAy2cjGZ_CUBQXzZhhWw9AtNHTU4jwhk4WizKRXbfuDvmRtAy1lPCnTKS6pLg
///Based on id and messageId, I am trying to download the attachment
GTLQueryGmail *query2 = [GTLQueryGmail queryForUsersMessagesAttachmentsGet];
query2.identifier=mailID;
query2.messageId=attachmentId;
[self.service executeQuery:query2
delegate:self
didFinishSelector:@selector(displayResultWithTicketOfDownloadAttachment:finishedWithObject:error:)];
}else{
UIAlertView *alertShow=[[UIAlertView alloc]initWithTitle:@"Error" message:@"No Attachment found" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertShow show];
}
}
- (void)displayResultWithTicketOfDownloadAttachment:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailListLabelsResponse *)labelsResponse
error:(NSError *)error {
NSLog(@"response is :%@",labelsResponse.JSON);
NSLog(@"error is :%@",error);
}
But I am getting error as : Error Domain=com.google.GTLJSONRPCErrorDomain Code=400 "The operation couldn’t be completed. (Invalid attachment token)" UserInfo=0x7b68e740 {error=Invalid attachment token, GTLStructuredError=GTLErrorObject 0x7b6876e0: {message:"Invalid attachment token" code:400 data:[1]}, NSLocalizedFailureReason=(Invalid attachment token)}
最佳答案
您的 ID 有点困惑。
GTLQueryGmail *query2 = [GTLQueryGmail queryForUsersMessagesAttachmentsGet];
query2.id=attachmentId;
query2.messageId=mailID;
您在响应中获得的数据是 Base64-encoded并通过将所有“+”替换为“-”并将所有“/”替换为“_”来使 URL 安全。
只需将其转换回常规 Base64 数据并解码即可!在您的开发者工具中试试这个(按 F12):
atob("SGV5IE5pbGVzaCEgSSBob3BlIHlvdSB3aWxsIGdldCBpdCB0byB3b3JrIHNvb24h".replace(/\-/g, '+').replace(/\_/g, '/'));
在您的代码中执行相同的步骤,您将获得原始格式的数据。
关于iOS - 使用 messageId 和 attachmentId 从 Gmail 下载附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578715/
我工作的公司评估了 MQTT,并决定将其用作大型系统的核心消息传递平台。主要原因是协议(protocol)的紧凑程度以及实际实现的容易程度。不过,我对 MQTT 有一个问题,我正在寻找以下问题的答案:
[FunctionName("MyProcess")] public static void Run(BrokeredMessage currentMessage,[ServiceBus("mytop
如何在 Java 的构造函数中将 Final int 初始化为比前一个实例大 1,我可以这样做吗?我的意思是我有一个 final int messageID; 它对于每个实例都必须是唯一的,我该怎么做
我为我公司的发票编写了一个自动电子邮件程序。在我遇到几封未送达的电子邮件之前,一切都很好。 经过进一步检查,我们的 IT 专家发现发件人发送的邮件具有不正确的 MessageID header 。 M
假设我有以下内容: [ { messageid: '582b4350af7cb8f21e4b7b43', _id: 582b4c79105387dd21e08004 } , {
我使用 PHPMailer 发送电子邮件,evthg 运行良好,但我会为每封电子邮件设置一个唯一的 MessageID。 PHPMailer 版本:“v5.2.16” (从 https://githu
我正在使用 IBM 的 mq 库从 MQ 队列中读取消息。现在我需要检索消息的 messageid。我现在它位于名称 messageId 下的消息 header 中。但这会返回一个 byte[]。现在
我正在使用 Mailcore2 从 IMAP 服务器获取邮件。如果我有邮件的 messageID 使用它,我可以获取用于 Outlook 和 Yahoo 邮件的文件夹吗? 我正在使用以下代码来获取消息
在 Spring AMQP 项目中, 如果 messageProperties 没有 messageId,它们总是创建 messageId。 像这样.. if (this.createMessageI
我刚刚设置了 phpmailer,我想在发送时将消息存储在日志表中。 phpmailer 自动创建一个 messageid,我如何检索 $MessageID 以便我可以将它存储在我的表中? 我不需要设
我有一个相当简单的函数,用于将消息发送到 azure 服务总线队列。 public async Task SendMessagesToQueue(string serviceBusConnection
我有一个相当简单的函数,用于将消息发送到 azure 服务总线队列。 public async Task SendMessagesToQueue(string serviceBusConnection
我正在使用 @JmsListener从队列接收所有消息的注释 @JmsListener(destination = MyQueue, selector = "MAINST = '" + instanc
我可以成功运行gcm的客户端和服务器。但是当我从服务器端发送消息时,我可以从 gcm 得到一个 messageId,它是这样的: 0:1408673696996426%921c249a002efde3
我正在发送一条 MQ 消息,并按预期返回一个 messageID 和 correlationID,我可以使用 MQ 服务器返回的 messageID 从我发送的特定消息中捕获响应。 我将我的应用程序放
我正在使用 PHP 和 IMAP 从 Gmail 中检索电子邮件;但是,有些电子邮件没有 messageid。不是所有消息都应该有一个 messageid 吗? 我需要一个唯一的 ID 供引用,所以我
我想保存已发送电子邮件的 MessageID,以便稍后在 References: header 中使用它以促进线程化。 我在 root/django/trunk/django/core/mail.py
标题总结了它。我需要从 Imap 文件夹中获取所有 MessageId 属性,而无需下载整封邮件。 ... IMailFolder inbox = imapClient.Inbox; SearchQu
我想自动化 Outlook,这样我就可以下载电子邮件的“片段”,这样我就可以将相关的邮件联系在一起。我知道电子邮件通常有一个“MessageID”来满足此目的,因此可以在上下文中查看电子邮件,因为新闻
我正在使用 MailKit/MimeKit 1.2.7(最新的 NuGet 版本)。 使用 ImapClient 删除电子邮件非常简单...... client.Inbox.AddFlags(uniq
我是一名优秀的程序员,十分优秀!