Posting an image to Facebook

Home / Forums / Marmalade Archive / General Development Questions / Posting an image to Facebook
3 replies [Last post]
by: TheMarshal99
Status: Offline
Joined: 2012-04-12
Points: 40

I would like to get my iPhone application to post a screenshot to Facebook, but it's not terribly clear from the Facebook Extension example how I would go about doing that.

After logging in, I do the following:

// mySession is the s3eFBSession that was returned after logging in.
s3eFBRequest req = s3eFBRequest_WithGraphPath(mySession, "me/photos", "POST");

// buff is a byte array containing png-formatting image data.
s3eFBRequest_AddParamString(req, "picture", buff);
s3eFBRequest_AddParamString(req, "message", "Check out my new game!");

The output I see in the console is:
FACEBOOK_VERBOSE: calling s3eFacebook func on main thread: s3eFBRequest_WithGraphPath
FACEBOOK_VERBOSE: New request (WithGraphPath): 0x... (0x...)
FACEBOOK_VERBOSE: Add Param String
FACEBOOK_VERBOSE: Add Param String
FACEBOOK_VERBOSE: calling s3eFacebook func on main thread: s3eFBRequest_Send
FACEBOOK_VERBOSE: Sending request
FACEBOOK_VERBOSE: requestWithGraphPath andParams andHttpMethod
FACEBOOK_VERBOSE: requestLoading(with url https://graph.facebook.com/me/photos)
FACEBOOK_VERBOSE: Request sent
FACEBOOK_VERBOSE: didReceiveResponse
FACEBOOK_VERBOSE: didLoadRawResponse
FACEBOOK_VERBOSE: Raw response {"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}

Interestingly enough, if I remove the "message" parameter, I get the following console output:
FACEBOOK_VERBOSE: calling s3eFacebook func on main thread: s3eFBRequest_WithGraphPath
FACEBOOK_VERBOSE: New request (WithGraphPath): 0x... (0x...)
FACEBOOK_VERBOSE: Add Param String
FACEBOOK_VERBOSE: calling s3eFacebook func on main thread: s3eFBRequest_Send
FACEBOOK_VERBOSE: Sending request
FACEBOOK_VERBOSE: requestWithGraphPath
FACEBOOK_VERBOSE: requestLoading(with url https://graph.facebook.com/me/photos)
FACEBOOK_VERBOSE: Request sent
FACEBOOK_VERBOSE: didReceiveResponse
FACEBOOK_VERBOSE: didLoadRawResponse
FACEBOOK_VERBOSE: Raw response {"data":[]}

Which leads me to believe that "picture" is not being properly added as a parameter (despite there being no error message thrown to that effect).

Am I missing something? Obviously adding the image data as a string doesn't seem to be the proper way of doing things, but that's all the Facebook extension seems to offer.

by: TheMarshal99
Status: Offline
Joined: 2012-04-12
Points: 40
I've verified on an Android

I've verified on an Android device running Java that not including the image data in the request will result in the "324" error that I was seeing above.

Unless someone has a better idea, I'm going to look into adding a s3eFBRequest_AddParamByteArray() method the s3eFacebook extension.

by: roo
Status: Offline
Joined: 2012-03-23
Points: 4040
I tried adding methods to

I tried adding methods to s3eFacebook and finished re-writting extension from scratch.
I have not uploaded photos but I think you should get access_token from extension and construct POST request manually
I believe it would be simpler then adding that feature to extension.

by: TheMarshal99
Status: Offline
Joined: 2012-04-12
Points: 40
Once I got my environment set

Once I got my environment set up, it wound up being trivial. Working in iOS, I added the following method to s3eFacebook_platform.mm:

s3eResult s3eFBRequest_AddParamByteArray_platform(s3eFBRequest* request, const char* name, const unsigned char* value, int length)
{
if (request && name && value)
{
IwTrace(FACEBOOK_VERBOSE, ("Add Param Byte Array"));

NSString *_key = ToNSString(name);
NSData *_value = [NSData dataWithBytes:value length:length];

[request->m_delegate addParamWithValue:_value forKey:_key];

return S3E_RESULT_SUCCESS;
}
return S3E_RESULT_ERROR;
}

The NSData type is properly recognized and handled as a special case in FBRequest.m where is adds the parameters. The image posted properly as a result. I haven't looked into the Android implementation, but it seems like overall this would have been a trivial thing to include in the default code.

Latest Posts

Latest Comments

Top Contributors

  • Chris D : 27,010
  • MonRoyals : 26,970
  • Salman : 26,720
  • Jez Hammond : 20,130
  • pjan : 11,150
  • gjw : 10,350
  • Paolo Oliverio : 9,640
  • Beaker_2 : 8,390
  • Kite : 8,210
  • bluescrn : 7,810