问题
I'm trying to request my company's Basecamp info for an internal project we're building. I understand how to add credentials in an ASP.NET environment but I'm new to iPad development and can't seem to get an appropriate response from Basecamp. Here's what I'm doing:
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mybasecampname.basecamphq.com/projects.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0 ];
I'm adding the HTTP headers that Bsaecamp requires like so:
[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type" ];
[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept" ];
I know I also need to send my credentials for authentication purposes - my authentication token and any password I like but I'm not sure the best way to do this. Here's what I'm trying:
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"MY TOKEN HERE"
password:@"x"
persistence:NSURLCredentialPersistenceForSession];
My question is: Am I on the right track here or am I missing something completely?
Here's a link to the Basecamp API details which explains what it needs: http://developer.37signals.com/basecamp/
Help appreciated.
Joe
回答1:
As is always the way, I worked it out eventually. Once I started using the didReceiveAuthenticationChallenge method things started to fall into place.
So. I simplified my request method:
- (void)startRequest
{
NSURL *url = [NSURL URLWithString:@"http://mybasecampproject.basecamphq.com/projects.xml"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
// Start the connection request
urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
and then set up a method for receiving the authentication challenge:
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Authentication challenge...");
NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:@"my basecamp token here" password:@"X"
persistence:NSURLCredentialPersistenceForSession] autorelease];
[[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
}
then set up a didReceiveData method to catch the data returned:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Did receive data");
NSString * strResult = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
NSLog(strResult);
}
Hope this helps someone else out there.
Still happy to hear better method for this
JJ
来源:https://stackoverflow.com/questions/3913269/how-do-i-set-basecamp-credentials-using-apple-ios-for-ipad