Posting user profile pic to twitter giving Error 131
Categories:
Resolving Twitter Error 131 When Posting User Profile Pictures on iOS 7

Encountering 'Error 131' when trying to upload a profile picture to Twitter from an iOS 7 app can be frustrating. This article delves into the common causes and provides practical solutions to help you successfully post user profile images.
Many developers integrating Twitter functionality into their iOS 7 applications have faced 'Error 131' when attempting to upload user profile pictures. This error typically indicates an issue with the image data itself, the way it's being transmitted, or limitations imposed by the Twitter API. Understanding the underlying causes is crucial for implementing a robust solution.
Understanding Twitter Error 131
Twitter's Error 131, often accompanied by a message like 'Could not authenticate you', 'Invalid or expired token', or 'Image data invalid', is a generic error that can stem from several issues related to image uploads. While the error message might suggest authentication problems, it frequently points to malformed image data or incorrect API usage, especially when dealing with profile pictures. On iOS 7, this often involves how UIImage
data is converted and sent via HTTP POST requests.
flowchart TD A[Start Profile Pic Upload] --> B{Convert UIImage to Data?} B -- Yes --> C[Check Data Format (JPEG/PNG)] C -- Valid --> D{Prepare HTTP POST Request} D --> E{Set Content-Type Header} E -- multipart/form-data --> F[Add Image Data to Body] F --> G{Send Request to Twitter API} G --> H{Receive Response} H -- Error 131 --> I[Troubleshoot: Data Format, Size, Headers] H -- Success --> J[Profile Pic Updated] B -- No --> I
Workflow for posting a profile picture to Twitter and potential error points.
Common Causes and Solutions
The primary culprits behind Error 131 when uploading images are usually related to the image data's format, size, or the HTTP request's structure. Here's a breakdown of common issues and their fixes:
1. Incorrect Image Data Conversion
The most frequent cause is improper conversion of UIImage
to NSData
. Twitter expects image data in a specific format, typically JPEG or PNG. Using UIImagePNGRepresentation
or UIImageJPEGRepresentation
is crucial, and ensuring the quality parameter for JPEG is appropriate can also prevent issues.
// Incorrect (or potentially problematic for Twitter API)
// NSData *imageData = UIImagePNGRepresentation(profileImage);
// Correct and recommended for Twitter profile pictures (JPEG)
NSData *imageData = UIImageJPEGRepresentation(profileImage, 0.8); // 0.8 is compression quality
// If PNG is preferred, ensure it's handled correctly by the API
// NSData *imageData = UIImagePNGRepresentation(profileImage);
Converting UIImage to NSData for Twitter upload.
2. Malformed HTTP POST Request
Twitter's API for media uploads often requires a multipart/form-data
content type. If you're not constructing your HTTP POST request correctly with the appropriate boundaries and headers, the server won't be able to parse the image data, leading to Error 131. Ensure your request body is correctly formatted to include the image data as a file part.
// Example of constructing a multipart/form-data request (simplified)
NSString *boundary = @"----------Boundary1234567890";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Add image data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"profile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Add other parameters if any
// [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\nTweet text\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Snippet demonstrating multipart/form-data request body construction.
3. Image Size and Dimensions
Twitter has specific requirements for profile picture dimensions and file size. While the exact limits can change, generally, profile pictures should be square and not excessively large in file size. If your image exceeds these limits, the API might reject it with Error 131. Resizing the image before conversion to NSData
is a good practice.
// Example of resizing a UIImage (simplified)
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
// Usage:
// UIImage *resizedImage = [self imageWithImage:profileImage scaledToSize:CGSizeMake(400, 400)];
// NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.8);
Helper method for resizing a UIImage before upload.
4. Authentication Issues (Less Common for Image Data)
Although Error 131 can indicate authentication problems, if you're successfully posting text tweets or other API calls, it's less likely to be the root cause for image uploads. However, always double-check that your OAuth credentials (consumer key, consumer secret, access token, access token secret) are correctly configured and signed for the request.
1. Step 1: Convert UIImage to JPEG Data
Ensure your UIImage
is converted to NSData
using UIImageJPEGRepresentation
with a reasonable compression quality (e.g., 0.8). This is generally more compatible with web services than PNG for profile pictures.
2. Step 2: Resize Image if Necessary
Before converting, resize your UIImage
to a suitable square dimension (e.g., 400x400 or 500x500 pixels) to meet Twitter's potential size constraints and reduce file size.
3. Step 3: Construct Multipart/Form-Data Request
Build your HTTP POST request with the Content-Type
header set to multipart/form-data
and include the image data as a file part within the request body, using appropriate boundaries.
4. Step 4: Verify API Endpoint and Authentication
Confirm you are using the correct Twitter API endpoint for profile picture updates and that your OAuth signing process is correctly implemented for the request.