Search code examples
iosobjective-ccurl

Box Api v2.0 transition from v1.0 Objective C Understanding


I am in the process of converting my Box v1.0 api calls to v2.0. I am having problems understanding Box's examples using 'Curl' and converting to current v1.0 'URL' code. I probably just don't understand 'Curl' more than the info that Box posted on developers site. Here is an Example:

current v1.0 api call to download a file.

NSString *urlPath = [NSString stringWithFormat:@"https://www.box.com/api/1.0/download/%@/%@",cfgBoxAuthToken,@"somefile"];

Box definition of how to do with v2.0 api call.

curl -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer ACCESS_TOKEN"

My attempt to convert to v2.0 api in Objective-C:

NSString *urlPath = [NSString stringWithFormat:@"https://api.box.com/2.0/files/%@/%@",@"somefile",cfgBoxAuthToken];

what am I doing wrong? I imagine that once I get this "Curl" example down I can convert my other Box calls.

Any help would be greatly appreciated!


Solution

  • Because their API is using OAuth2, they're requiring the Authorization header to be in your API call. -H in CURL passes header elements.

    You need to now define the url independently from the header, using something like:

    NSString *authHeader = [NSString stringWithFormat:@"Bearer %@", accessToken];

    More information on passing headers with API calls can be found here: Basic authorization in NSMutableURLRequest isn't working