2012-08-29 45 views

回答

0

下面是YouTube上的客戶端的登錄方法:

- (BOOL)login:(NSString *)strusername password:(NSString *)strpassword 
{ 
    NSMutableURLRequest *request = [NSMutableURLRequest 
           requestWithURL:[NSURL URLWithString:@"https://www.google.com/accounts/ClientLogin"]]; 

    NSString *params = [[NSString alloc] initWithFormat:@"Email=%@&Passwd=%@&service=youtube&source=&continue=http://www.google.com/",username,password]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 
    [params release]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"]; 

    NSHTTPURLResponse *response; 
    NSError *error; 
    [request setTimeoutInterval:120]; 
    NSData *replyData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  

    NSString *replyString = [[[NSString alloc] initWithData:replyData encoding:NSUTF8StringEncoding] autorelease]; 
    if([replyString rangeOfString:@"Auth="].location!=NSNotFound) 
    { 
    NSString *authToken=[[replyString componentsSeparatedByString:@"Auth="] objectAtIndex:1]; 
    NSLog(@"Auth : %@",authToken); 
    return YES; 
    } 
    else 
    { 
    return NO; 
    } 
} 
相關問題