tags:

views:

799

answers:

2

I'm trying to implement Google Friend Connect as a sign in solution. I've implemented Facebook Connect using the PHP client, and I'd like to use the same approach for Google Friend Connect (OpenSocial), using the opensocial-php-client (http://code.google.com/p/opensocial-php-client/). Once the user has connected, I'd like to get their opensocial id and log it into the database.

Here's the code so far:

$gfc_provider = new osapiFriendConnectProvider();
$gfc_auth = new osapiOAuth2Legged(GFCAPIKEY, GFCSECRET);
$gfc_osapi = new osapi($gfc_provider, $gfc_auth);
$batch = $gfc_osapi->newBatch();
$batch->add($gfc_osapi->people->get(array('userId' => '@me')));
$result = $batch->execute();
print_r($result);

Here's the response:

Array (
[0] => osapiError Object (
[errorCode:private] => 400 
[errorMessage:private] =>
    Cannot ask for me when anonymous
    Error 400
[response] => 
) 
)

Not sure what I'm doing wrong....any suggestions?

A: 

FYI, the discussion has moved to the opensocial-client-libraries Google Group (http://groups.google.com/group/opensocial-client-libraries/browse_thread/thread/9976bf92a13be775)

timborden
+2  A: 

For those looking to use Google Friend Connect in the same way you can use Facebook Connect, here is how I got it done (with the help of Arne). I've included Facebook Connect comparisons:

FB (php):

$facebook = new Facebook(FBAPIKEY, FBSECRET);
$facebook_id = $facebook->get_loggedin_user();

GFC (php):

if ($_COOKIE["fcauth" . GFCSITEID] != ''){
    $gfc_provider = new osapiFriendConnectProvider();
    $gfc_auth = new osapiFCAuth($_COOKIE["fcauth" . GFCSITEID]);
    $gfc_osapi = new osapi($gfc_provider, $gfc_auth);
    $batch = $gfc_osapi->newBatch();
    $batch->add($gfc_osapi->people->get(array('userId' => '@me')));
    $result = $batch->execute();
    $opensocial_id = $result[0]['data']->id;
}

(Please note, that you need the updated files, provided by Arne from the link above, to use the osapiFCAuth object)

FB (js):

FB.init("XXXXXXXXXXXXXXXXXXXXXXX", "xd_receiver.htm", {"reloadIfSessionStateChanged":true});

GFC (js):

google.friendconnect.container.setParentUrl('/');
google.friendconnect.container.initOpenSocialApi({
    site: 'XXXXXXXXXXXXXXXXXXXXX',
    onload: function(securityToken) {
     var req = opensocial.newDataRequest();
     req.add(req.newFetchPersonRequest('VIEWER'), "viewer");
     req.send(function(response) {
      var data = response.get('viewer').getData();
      if (data){
       var opensocial_id = data.getId();
       if (opensocial_id && $("div#gfc").length > 0) window.location.reload();
      }
     });
    }
});
if ($('div#gfc').length > 0) google.friendconnect.renderSignInButton({'id':'gfc', 'text':'Connect with Google', 'style':'long'});

FB (html):

<fb:login-button size="medium" length="long"></fb:login-button>

GFC (html):

<div id="gfc"></div>
timborden