开发者

Profile Image Update Twitter API

I am trying to upload a profile image to twitter using their API. I found some good examples on the web on how to format the URLRequest, however I just can't seem to get this to work. Any help would be appreciated. The URL that I am using from Twitter API is 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.networkActivityIndicatorVi开发者_Go百科sible = 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");

    }
}


Json doesnt work use xml instead

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜