I've been using this SO post (iOS - download attachment from Gmail using messageId and attachmentId) as a reference for
I've been able to make the API call, get the response data and parse out the data for the attachment:
AF.request("https://www.googleapis.com/gmail/v1/users/me/messages/\(email.id)/attachments/\(email.payload.body.attachmentId)", headers: headers)
.responseJSON
{ [self] response in
do {
let json = try JSON(data: response.data!)
let jsonDict = convertToDictionary(text: json.rawString()!)
let attachmentDataString: String? = jsonDict!["data"] as? String
Following the accepted answer in the linked post, I made sure to format my string so that it was base64 compatible:
let formattedString = attachmentDataString!.replacingOccurrences(of: " ", with: "+", options: .literal, range: nil)
And then attempt to convert that string to data:
let attachmentData = Data(base64Encoded: formattedString)
However, this object is always nil (I'm pre-checking to make sure I only run this logic on emails with attachments).
Any help appreciated!
EDIT: This is the data string before my formatting:
https://gist.github.com/narner/d7805807992fa44a30e00d6480a04164
And this is the data string after my formatting:
https://gist.github.com/narner/02c3152d1dc935985a4da8d9b7388ade