Posting images to facebook?

Has anybody done any work with posting images to Facebook from openframeworks? There doesn’t seem to be a really good api out there that I can find.

If anybody else wants to be able to do this:

Setup your application in Facebook. Make sure you read the docs and set permissions as needed. I used the graph api. The photo kiosk I’m working on requires offline access, which is an option set in the app authorization stage. Then you’ll need curlpp and the code posted here:

http://forum.developers.facebook.com/vi-…-21#p234221

Feel free to email me if you need help with this.

Hi Jaypozo

the facebook forums no longer exist. Can you tell me how you managed to post to a users facebook wall. More importantly, the authentication side of things

Cheers

hi jaypozo, hi az_rr

Did you post photo to FB without any offscreen browser?

I have searched graph api but could not find the direct way to get access_token from within third party app…

So I did it with ofxBerkelium and curl like this

  1. open Berkelium browser inside the oF app
  2. Redirect the user to FB login page using the URL below:
    https://www.facebook.com/dialog/oauth?client-id=YOUR-APP-ID&redirect-uri=https://www.facebook.com/connect/login-success.html
  3. Server returns authorization code
  4. Request access_token from Facebook using the URL below:
    https://graph.facebook.com/oauth/access-token?client-id=YOUR-APP-ID&redirect-uri=https://www.facebook.com/connect/login-success.html&client-secret=YOUR-APP-SECRET&code=AUTHORIZATION-CODE
  5. Server returns access_token
  6. post image and access_token to "https://graph.facebook.com/me/photos" using curl

This flow works, but sooooo tricky.
Is there any simple approach…?

Thanks

A

You wouldn’t be able to share some of your code would you Akira?

Im having a bit of trouble

Hello Aaron,

OK. But I think my code is not the best approach
There should be more simple one.
Since the project is huge, I extract the essence of the code.
So this is kind of pseudo code.

The process has 2 big steps.

  1. get access token.
  2. post something to the FB

(I tried these steps without Berkelium (only curl), but curl does not work on step 1…
It should work, but did not. I am not sure why.)

  • open Berkelium browser
  
const string AUTH_CODE_TARGET_URL = "[https://www.facebook.com/dialog/oauth?client-id=";](https://www.facebook.com/dialog/oauth?client-id=";)  
const string AUTH_CODE_REDIRECT_URL = "&redirect_uri=[https://www.facebook.com/connect/login-success.html";](https://www.facebook.com/connect/login-success.html";)  
const string AUTH_CODE_PERMISSION_SCOPE = "&scope=publish_stream";  
  
void openBrowser()  
{  
	string url = AUTH_CODE_TARGET_URL + YOUR_APP_ID + AUTH_CODE_REDIRECT_URL + AUTH_CODE_PERMISSION_SCOPE;  
	browser->getWindow()->navigateTo(url.data(), url.length());  
}  

  • user input userID and password.

  • on Berkelium’s callback function,
    ---- get authCode.
    ---- request “access token request url”
    ---- get “access token request url”
    ---- request ACCESSTOKEN

  
const string ACCESS_TOKEN_TARGET_URL = "[https://graph.facebook.com/oauth/access-token?client-id=";](https://graph.facebook.com/oauth/access-token?client-id=";)  
const string ACCESS_TOKEN_REDIRECT_URL = "&redirect_uri=[https://www.facebook.com/connect/login-success.html&client-secret=";](https://www.facebook.com/connect/login-success.html&client-secret=";)  
const string AUTH_CODE_PREFIX = "&code=";  
const string AUTH_CODE_RETURNING_GARBAGE = "[https://www.facebook.com/connect/login-success.html?code=";](https://www.facebook.com/connect/login-success.html?code=";)  
const string ACCESS_TOKEN_RETURNING_GARBAGE = "[https://graph.facebook.com/oauth/access-token?";](https://graph.facebook.com/oauth/access-token?";)  
  
void onAddressBarChanged(string newURL)   
{  
	// get authorization code. Then request "access token request url".   
	if (ofIsStringInString(newURL, AUTH_CODE_RETURNING_GARBAGE))   
	{	  
		authCode = newURL;  
		authCode.erase(0, AUTH_CODE_RETURNING_GARBAGE.length());  
		string tokenURL = ACCESS_TOKEN_TARGET_URL + YOUR_APP_ID +   
						  ACCESS_TOKEN_REDIRECT_URL + YOUR_APP_SECRET +   
						  AUTH_CODE_PREFIX + authCode;  
		browser->getWindow()->navigateTo(tokenURL.data(), tokenURL.length());  
		bDrawBrowser = false;  
	}  
	  
	// get the "access token request url". Then request access token to that url with curl.  
	if (ofIsStringInString(newURL, ACCESS_TOKEN_RETURNING_GARBAGE) && !bDoneAccessTokenRequest)   
	{	  
		accessTokenURL = newURL;  
		bDoneAccessTokenRequest = true;  
		  
		accessTokenURL.replace(accessTokenURL.length()-2, accessTokenURL.length(), "");  
		  
		CURL* easy_handle;  
		CURLcode result;  
		easy_handle = curl_easy_init();  
		curl_easy_setopt(easy_handle, CURLOPT_URL, accessTokenURL.c_str());  
		curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, curlWriter);  
		curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &ACCESSTOKEN);  
		curl_easy_perform(easy_handle);  
		curl_easy_cleanup(easy_handle);  
  
		string prefix = "access_token=";		  
		string delim = "&";		  
		string expire = "expires";  
		  
		ACCESSTOKEN.erase(0, prefix.length());  
		vector<string> split = ofSplitString(ACCESSTOKEN, delim);  
		for (int i = 0; i < split.size(); i++)   
		{  
			if (!ofIsStringInString(split[i], expire))   
			{  
				ACCESSTOKEN = split[i];  
				break;	  
			}  
		}  
		  
	}  
}  

  1. post image to FB with ACCESSTOKEN
  
void faceBookPost()   
{	  
	string path = PATH_TO_YOUR_IMAGE;  
	  
	struct curl_httppost *post=NULL;  
	struct curl_httppost *last=NULL;  
	struct curl_slist *headers=NULL;  
	  
	CURL * easyhandle;  
	easyhandle = curl_easy_init();  
	curl_easy_setopt(easyhandle, CURLOPT_VERBOSE, 1); // do this to make debugging easier  
	  
	headers = curl_slist_append(headers, "Content-Type: multipart/form-data");  
	  
	curl_formadd(&post, &last,  
				 CURLFORM_COPYNAME, "access_token",  
				 CURLFORM_COPYCONTENTS, ACCESSTOKEN.c_str(), CURLFORM_CONTENTHEADER, headers, CURLFORM_END);  
	  
	curl_formadd(&post, &last,  
				 CURLFORM_COPYNAME, "file",  
				 CURLFORM_FILE, path.c_str(), CURLFORM_CONTENTTYPE, "image/jpg", CURLFORM_END);  
	  
	curl_formadd(&post, &last,  
				 CURLFORM_COPYNAME, "message",  
				 CURLFORM_COPYCONTENTS, commentToPost.c_str(), CURLFORM_CONTENTHEADER, headers, CURLFORM_END);  
	  
	  
	curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, "");  
	curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDSIZE, 23L);  
	curl_easy_setopt(easyhandle, CURLOPT_URL, "[https://graph.facebook.com/me/photos"](https://graph.facebook.com/me/photos"));  
	  
	/* Set the form info */  
	curl_easy_setopt(easyhandle, CURLOPT_HTTPPOST, post);  
	  
	/* post away! */  
	curl_easy_perform(easyhandle);   
	  
	/* free the post data again */  
	curl_formfree(post);  
	  
	curl_easy_cleanup(easyhandle);  
	  
}  

Hope this help!

A

A lazy way to post images to facebook from an OF app is to use facebook’s ‘upload by email’ secret address (check your facebook settings to find it). You don’t need tokens or any authentication, you just need to know how to send emails from OF or call a script to send the email. I use this method all the time… and im lazy.

Hi

Im looking for a way to post images to facebook from an of app on osx. I see that are several addons to do that for iOS but i dont found nothing to do on desktop.

Also i read about the social.framework on osx but dont find any implementation on oF

Any clue with be wellcome :slight_smile: