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 library (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] => 
) 
) 

I am not sure what I'm doing wrong. Any suggestions?

4

2 回答 2

3

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:

Facebook (PHP):

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

Google Friend Connect (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)

Facebook (JavaScript):

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

Google Friend Connect (JavaScript):

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'}); 

Facebook (HTML):

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

Google Friend Connect (HTML):

<div id="gfc"></div>
于 2009-06-11T19:48:37.640 回答
0

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

于 2009-06-05T15:49:18.727 回答