Facebook plugin - get list of friends

Congratulations for the new cocos2d-x Facebook API !

Is there a way to get the list of friends using the Facebook plugin?

For iOS SDK

But I was wondering how can I do the same using the cocos2d-x API,

After getting the list of friends how can I post a message or link at any friend’s wall ?

Thanks

1 Like

I added this code

void layer_basic_t::facebook_login_permissions()
{
 
  std::string permissions = “user_friends,publish_actions,user_about_me”;
  FacebookAgent::getInstance()->login(permissions, [=](int ret, std::string& msg)
  {
    CCLOG("%s", msg.c_str());
  });

}

void layer_basic_t::facebook_api()
{
  facebook_login_permissions();

std::string path = “/me/invitable_friends”;
  FacebookAgent::FBInfo params;
  FacebookAgent::getInstance()->api(path, FacebookAgent::HttpMethod::Get, params, [=](int ret, std::string& msg)
  {
    CCLOG("%s", msg.c_str());
  });

}

but I get an error message

cocos2d: {“error_message”:“Error Domain=com.facebook.sdk Code=5 “The operation couldn’t be completed. (com.facebook.sdk error 5.)” UserInfo=0x7d8a6890 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={\n    body =     {\n        error =         {\n            code = 15;\n            message = “(#15) This method is only accessible to Games on Facebook.com - please set a Canvas URL in your app’s settings: https://developers.facebook.com/apps/908618432484237/settings”;\n            type = OAuthException;\n        };\n    };\n    code = 400;\n}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0x7ae6b8d0, state: FBSessionStateOpen, loginHandler: 0x7ae64360, appID: 908618432484237, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x7ae7d130>, expirationDate: 2015-01-16 00:39:14 +0000, refreshDate: 2014-11-17 06:56:46 +0000, attemptedRefreshDate: 0000-12-30 00:00:00 +0000, permissions:(\n    “public_profile”,\n    “manage_pages”,\n    “publish_actions”,\n    “user_activities”,\n    “user_friends”,\n    “user_about_me”\n)>}”}
cocos2d: {“permissions”:[“public_profile”,“manage_pages”,“publish_actions”,“user_activities”,“user_friends”,“user_about_me”],“accessToken”:“CAAM6Yjbnk40BAGZBNRYZAT2eKMZBinP7Kguv6G60o1qgTuxStKXu66MUeXcFbZBor0Y32y2OYVqYPO7niHprLrI3ZApZBXymniuGZAZAEHDvZCZALfvfoUzU6banLQ5wS14Rh6jkTuZCkBcfIV2aPxCIzi6kMbYXrtELxu2tWu8ZChZCTB1mXyQHGst7iJx2ZAZAzvnWRZCKgyO7yHfUxLIhoVmS9pintHQiZAFr63yZ”}

to use “me/inviatable_friends” you must have to create facebook canvas app in facebook developer account.


you can use “me/friends”, it will give you the list of friends all installed the same app.

Doesn’t that mean that you also have to create the canvas app?
https://developers.facebook.com/docs/games/canvas

Or will it work as long as you don’t publish the canvas app?

you have to add canvas app in developer console only, while creating you can provide your site link or any other link.
It will work, It is not required to create actual canvas app and publish it.

It is compulsory only if you submit your app for facebook review.

Thanks lazydevx, I’ll try that out tonight then.

You wouldn’t happen to know if the share dialog failing is related somehow to this as well, would you?
http://discuss.cocos2d-x.org/t/facebook-plugin-error-share-failed-dialog-not-supported-feed-dialog
Because if that issue is due to not having the correct permissions, then you have to submit your app to facebook to get them. How would you go about that then, temporarily delete the canvas?
Or can you submit only the android/ios app for review (when I was toying with the page, I only saw the option to submit all for review…)

Thanks for the help!

please try this code

        FacebookAgent::FBInfo params;
        params.insert(std::make_pair("name", "My Game"));
        params.insert(std::make_pair("caption", "New Coocs2dx game"));
        params.insert(std::make_pair("description", "My Game! Built buy cocos2dx."));
        params.insert(std::make_pair("link", "http://www.cocos2d-x.org"));
        params.insert(std::make_pair("picture", "http://your.game.icon.link"));
        if (FacebookAgent::getInstance()->canPresentDialogWithParams(params))
        {
            FacebookAgent::getInstance()->dialog(params, [=](int ret ,std::string& msg)
                                                 {
                                                     CCLOG("%s", msg.c_str());
                                                 });
        }
        else
        {
            FacebookAgent::getInstance()->share(params, [=](int ret ,std::string& msg)
                                                 {
                                                     CCLOG("%s", msg.c_str());
                                                 });
        }

and for this you don’t have to request any other permissions.

Thanks lazydevx, I will try it and post results here.

That works great, thanks a ton.

The only thing I don’t seem to be able to get working now, is to insert a screenshot into the share.
But I won’t hijack this thread any further :wink:

Thanks again for the help!

That worked, it falls on the “share” call.
I used

void layer_basic_t::facebook_post()
{
  facebook_login(); 
  FacebookAgent::FBInfo params;    
  params.insert(std::make_pair("name", "Word Build"));
  params.insert(std::make_pair("caption", "New Coocs2dx game"));
  params.insert(std::make_pair("description", "My Game! Built buy cocos2dx."));
  params.insert(std::make_pair("link", "http://www.cocos2d-x.org"));
  params.insert(std::make_pair("picture", "http://files.cocos2d-x.org/images/orgsite/logo.png"));  
  if (FacebookAgent::getInstance()->canPresentDialogWithParams(params))
  {
    FacebookAgent::getInstance()->dialog(params, [=](int ret, std::string& msg)
    {
      CCLOG("%s", msg.c_str());
    });
  }
  else
  {
    CCLOG("Can't open dialog for message_link");   
    FacebookAgent::getInstance()->share(params, [=](int ret ,std::string& msg)
    {
      CCLOG("%s", msg.c_str());
    });
  }
}

I have the message

CCLOG(“Can’t open dialog for message_link”);

not sure why, could it be that needs Facebook review/approval of the app?

try this code

        FacebookAgent::FBInfo params;
        **params.insert(std::make_pair("dialog", "messageLink"));**
        params.insert(std::make_pair("name", "My Game"));
        params.insert(std::make_pair("caption", "New Coocs2dx game"));
        params.insert(std::make_pair("description", "My Game! Built buy cocos2dx."));
        params.insert(std::make_pair("link", "http://www.cocos2d-x.org"));
        params.insert(std::make_pair("picture", "http://your.game.icon.link"));
        if (FacebookAgent::getInstance()->canPresentDialogWithParams(params))
        {
           //this will work only on real device which have Facebook app installed
            FacebookAgent::getInstance()->dialog(params, [=](int ret ,std::string& msg)
                                                 {
                                                     CCLOG("%s", msg.c_str());
                                                 });
        }
        else
        {
            FacebookAgent::getInstance()->share(params, [=](int ret ,std::string& msg)
                                                 {
                                                     CCLOG("%s", msg.c_str());
                                                 });
        }

it has nothing to do with facebook review or approval

to share screenshot try this one

    FacebookAgent::FBInfo params;
    params.insert(std::make_pair("dialog", "sharePhoto"));
    params.insert(std::make_pair("photo", outputFile));

//param required for else block only
params.insert(std::make_pair(“name”, “My Game”));
params.insert(std::make_pair(“caption”, “New Coocs2dx game”));
params.insert(std::make_pair(“description”, “My Game! Built buy cocos2dx.”));
params.insert(std::make_pair(“link”, “http://www.cocos2d-x.org”));
params.insert(std::make_pair(“picture”, “http://files.cocos2d-x.org/images/orgsite/logo.png”));

    if (FacebookAgent::getInstance()->canPresentDialogWithParams(params))
    {
        FacebookAgent::getInstance()->dialog(params, [=](int ret ,std::string& msg)
                                             {
                                                 CCLOG("%s", msg.c_str());
                                             });
    }
    else
    {
        FacebookAgent::getInstance()->share(params, [=](int ret ,std::string& msg)
                                            {
                                                CCLOG("%s", msg.c_str());
                                            });
    }

this should work only when fb app is installed in the device.

Oh man… You’re a life saver…
You know what I had wrong? I was using share_photo instead of sharePhoto.
Now this works as well…

The only thing left to do now, is store and retrieve data from facebook (like the gold bars in candy crush), but I think that’s the easiest part. I think you get that info when you login, but I have to read up on that now.

Again though, thanks for all the help lazydevx!

@MFHSchoonbrood

You Welcome.

The screenshot sharing works when I run the app on my tablet from eclipse.
But when I export the game as apk and install it on the device, sharing fails…

Any idea why this is?
Do I have to make the game live on facebook before my testers can test the functionality?

EDIT1:
I do see this in the logcat when I try to post:
com.facebook.photos.upload.uploaders.PartialUploadException: com.facebook.http.protocol.ApiException: [code] 404 [message]: (#404) Key hash 4DoxSomeMoreNumbersxDoP8 does not match any stored key hashes. [extra]:

My facebook app status has the option:
Do you want to make this app and all its live features available to the general public?
And it’s set to NO.
I didn’t set it to YES yet, because my game is still in testing… And when I click yes, it says my game becomes public and available to everyone.

I guess that I just have to set it to yes and try it out :slight_smile:

EDIT2:
I think the facebook app doesn’t need to be live, I think the issue is that the release key I extracted doesn’t match the hash key the device is reporting.
I found people with similar issues here: http://stackoverflow.com/questions/20506658/android-facebook-sdk-key-hash-does-not-match-any-stored-key-hashes-when-uploadi

I’ll update once I get it working…

have you entered your hash key in facebook developer console

Hehe, you replied while I was editing my post :smile:
I think the issue is that the extracted key doesn’t match the key being reported in my logcat, so I’m updating that now.

EDIT: My hash keys were indeed wrong because I was using the latest openssl from google (openssl-0.9.8k_X64.zip). After switching to an older version (openssl-0.9.8e_X64.zip), I got the same key that was reported in my crash report…

So don’t use google openssl version 098 k, use d or e instead!

Hi! with the else case:

//param required for else block only
params.insert(std::make_pair("name", "My Game"));
params.insert(std::make_pair("caption", "New Coocs2dx game"));
params.insert(std::make_pair("description", "My Game! Built buy cocos2dx."));
params.insert(std::make_pair("link", "http://www.cocos2d-x.org"));
params.insert(std::make_pair("picture", "http://files.cocos2d-x.org/images/orgsite/logo.png"));

and

else
    {
        FacebookAgent::getInstance()->share(params, [=](int ret ,std::string& msg)
                                            {
                                                CCLOG("%s", msg.c_str());
                                            });
    }

how can i share a photo from my smartphone? with picture and link Only “http” or “https” schemes are supported https://developers.facebook.com/docs/reference/ios/current/class/FBLinkShareParams

with

params.insert(std::make_pair("photo", outputFile));

not works without Dialog and so facebook app installed

Try this to share an image on android (like a screenshot):

cocos2d::plugin::FacebookAgent::FBInfo params;
params.insert( std::make_pair( "dialog", "sharePhoto" ) );
params.insert( std::make_pair( "photo", photoFilename ) );
if (m_pFacebookAgent->canPresentDialogWithParams(params))
{
    m_pFacebookAgent->dialog(params, [=](int ret, std::string& msg)
}

And try this to share a link:

cocos2d::plugin::FacebookAgent::FBInfo params;
params.insert( std::make_pair( "dialog", "shareLink" ) );
params.insert( std::make_pair( "name", name ) );
params.insert( std::make_pair( "caption", caption ) );
params.insert( std::make_pair( "description", description ) );
params.insert( std::make_pair( "link", link ) );
params.insert( std::make_pair( "picture", pictureLink ) );
if (m_pFacebookAgent->canPresentDialogWithParams(params))
{
    m_pFacebookAgent->dialog(params, [=](int ret, std::string& msg)
}

You have to replace the param values, as they are function parameters in my code, like this:

bool Facebook::ShareLink( const char* name, const char* caption, const char* description, const char* link, const char* pictureLink )

And m_pFacebookAgent is the facebook agent, so you can store it like I did, or just use cocos2d::plugin::FacebookAgent::getInstance()

Good luck!

Hi everyone,
I tried lazydevx code, and it’s work well when user have the native Facebook app installed.
So, how can i share a photo when user didn’t get Facebook app. Any suggestions ?