Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-使用messageId和attachmentId从Gmail下载附件_Ios_Gmail Api - Fatal编程技术网

iOS-使用messageId和attachmentId从Gmail下载附件

iOS-使用messageId和attachmentId从Gmail下载附件,ios,gmail-api,Ios,Gmail Api,我在我的iOS项目中遵循Google文档,我需要使用它的userId、messageId和id从邮件中下载附件。 其中userId是用户的电子邮件地址, id是mailId() messageId是我们通过使用上面带有附件的id之一获得的 这是我目前掌握的代码: - (void)fetchLabels { GTLQueryGmail *query = [GTLQueryGmail queryForUsersThreadsList]; [self.service executeQu

我在我的iOS项目中遵循Google文档,我需要使用它的userId、messageId和id从邮件中下载附件。 其中userId是用户的电子邮件地址, id是mailId() 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

你把身份证弄混了

GTLQueryGmail *query2 = [GTLQueryGmail queryForUsersMessagesAttachmentsGet];
        query2.id=attachmentId;
        query2.messageId=mailID;
通过将所有“+”替换为“-”,并将所有“/”替换为“\”,您在响应中获得的数据是并且已经是URL安全的

只需将其转换回常规Base64数据并解码即可!在开发人员工具中尝试此操作(按F12):


在代码中执行相同的步骤,您的数据将以原始格式显示。

您可以记录
query2
?我很想看看它的样子。GTLQueryMail*query2=[GTLQueryMail QueryForUsersMessageAttachmentsGet];NSLog(@“第一个日志:%@”,查询2);query2.identifier=mailID;query2.messageId=attachmentId;NSLog(@“第二个日志:%@”,查询2);第一个日志:GTLQueryMail 0x7994bd20:{method:gmail.users.messages.attachments.get}第二个日志:GTLQueryMail 0x7994bd20:{method:gmail.users.messages.attachments.get params:{method:gmail.messages.attachments:{method:gmail.users.messages.attachments.get params:(id,messageId)}这太酷了,伙计…..因为命名约定,我不知道哪里出错了……再次感谢:)没问题,尼利斯!很高兴我能帮忙。:)你好,Thole……在获得“数据”后,你能建议我现在该怎么做吗?@NileshKumar编辑了回复。在这个话题上,你好需要更多的帮助。
GTLQueryGmail *query2 = [GTLQueryGmail queryForUsersMessagesAttachmentsGet];
        query2.id=attachmentId;
        query2.messageId=mailID;
atob("SGV5IE5pbGVzaCEgSSBob3BlIHlvdSB3aWxsIGdldCBpdCB0byB3b3JrIHNvb24h".replace(/\-/g, '+').replace(/\_/g, '/'));