I am looking at building a simple cocoa app to create projects in the collaborative app Asana.
I need to make a curl request but I am unsure of how to tackle it.
Example(https://asana.com/developers/api-reference/projects):
curl -u <api_key>: https://app.asana.com/api/1.0/projects -d "name=Things to Buy" -d "notes=These are things we want to purchase." -d "workspace=14916"
Any ideas of how I can run or mimic a "curl -u" command?
Thanks!
Adam
The -u/--user
option stands for Basic Authentication. It basically adds a Authorization
HTTP header to the request, with the username/password encoded in base64, e.g Authorization: Basic Zm9vOmJhcg==
.
Basic auth is precisely one of the authentication scheme supported by the Asana API:
The Asana API supports two separate authentication schemes: OAuth 2.0 and HTTP Basic Authentication using API keys.
In practice, and according to the Asana docs, the clients should pass:
the API key as the username, and an empty password
So If you want to perform such a request in plain Objective-C e.g with NSURLConnection
all you need to do is create this Authorization
header yourself, taking care to use an empty password. You should be able to do this easily (see this answer):
NSString *authStr = [NSString stringWithFormat:@"%@:", apiKey];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
Note: base64 encoding is achieved via an NSData
category such as this one.