Handling NSData response from Google Docs API in iOS app

I’m developing an iOS app that uses the Google Docs API. After sending a POST request I got some NSData back. When I log it I see this:

let returnedData = URLSession.shared.sendSynchronousRequest(request, returningResponse: &theResponse, error: nil)
print(returnedData)
// Output: <4572726f 723d4261 64417574 68656e74 69636174 696f6e0a>

I think this might be a dictionary or an array. I’m expecting to receive an SID, an LSID, and an Auth. How can I convert this NSData into a format that allows me to access the items by key?

Any suggestions on parsing this response would be much appreciated. Thanks for your help!

hey there, i’ve dealt with similar stuff before. looks like you’re getting an error message, not the data you want. that hex stuff decodes to ‘Error=BadAuthentication’. double-check your auth setup, might be an issue there.

to read it properly, try this:

if let str = String(data: returnedData, encoding: .utf8) {
print(str)
}

hope that helps!

Having worked extensively with Google APIs, I can tell you that the NSData you’re seeing is indeed encoded. However, it’s not a dictionary or array as you might expect. It’s actually a hexadecimal representation of ASCII characters.

In your case, the decoded message is ‘Error=BadAuthentication’. This suggests there’s an issue with your authentication process. Double-check your API credentials and ensure you’re following Google’s OAuth flow correctly.

To decode the NSData, use:

if let responseString = String(data: returnedData, encoding: .utf8) {
    print(responseString)
}

If authentication succeeds, you’ll receive a string with key-value pairs. Parse this string to extract your SID, LSID, and Auth. Consider using regular expressions or string manipulation methods to achieve this.

Remember to implement proper error handling to manage cases like the one you’re encountering.

I’ve encountered a similar situation when working with Google APIs. The NSData you’re seeing is actually a hexadecimal representation of ASCII characters. In this case, it translates to ‘Error=BadAuthentication’.

To properly handle this, you should convert the NSData to a string first. Here’s what I typically do:

if let responseString = String(data: returnedData, encoding: .utf8) {
    print(responseString)
}

This will give you a readable string. From there, you can parse it as needed. If it’s a successful response, it’ll likely be in a key-value format. You might need to split the string by lines and then by ‘=’ to get your SID, LSID, and Auth.

Remember to handle potential errors, as your current output suggests an authentication issue. Make sure your credentials are correct and you’re following Google’s authentication flow properly. Good luck with your project!