Обновление профиля изображения Twitter API - PullRequest
0 голосов
/ 18 января 2010

Я пытаюсь загрузить изображение профиля в твиттер, используя их API. Я нашел несколько хороших примеров в Интернете о том, как отформатировать URLRequest, однако я просто не могу заставить это работать. Любая помощь будет оценена. URL, который я использую в Twitter API: http://twitter.com/account/update_profile_image.json

-(void)uploadImageForAccount:(NSURL *)theURL intUserName:(NSString *)_userName initPassWord:(NSString *)_passWord image:(NSData *)image{
    userName = [_userName retain];
    passWord = [_passWord retain];

    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    requestedData = [[NSMutableData alloc] initWithLength:0];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                                                          timeoutInterval:60];

    [theRequest setHTTPMethod:@"POST"];

    NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    [theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

    // Credentials
    NSString *creds = [NSString stringWithFormat:@"%@:%@", userName, passWord];
    [theRequest addValue:[NSString stringWithFormat:@"Basic %@",[self base64encode:creds]] forHTTPHeaderField:@"Authorization"];

    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"canary"] dataUsingEncoding:NSUTF8StringEncoding]];

    NSString *mimeType = mimeType = @"image/jpeg";

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"TNImage.jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:image];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [theRequest setHTTPBody:postBody];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest 
                                                                  delegate:self 
                                                          startImmediately:YES];

    if(DEBUG) NSLog(@"Connection = %@",[connection description]);
    if (connection == nil) {
        if(DEBUG) NSLog(@"Connection was Nil");

    }
}

1 Ответ

0 голосов
/ 18 января 2010

Json не работает, вместо этого используется xml

...