Google Friend Connect, get viewers id...

36 views
Skip to first unread message

timborden

unread,
Jun 5, 2009, 11:08:10 AM6/5/09
to opensocial-client-libraries
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. 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?

(Disclosure: i've asked the same question in the StackOverflow
community: http://stackoverflow.com/questions/955895/google-friend-connect-opensocial-php-client)

Arne Roomann-Kurrik

unread,
Jun 5, 2009, 11:24:16 AM6/5/09
to opensocial-client-libraries
Hi Tim,

2 legged OAuth is used to make requests on behalf of the site and not
a specific user. When you authenticate in this way, there's no means
to determine the currently logged in user, so any requests that need a
user's context (anything that uses @me, for example) won't work.

The PHP client supports explicitly setting a user context for 2-legged
by passing an additional parameter:
$auth = new osapiOAuth2Legged("<consumer key>", "<consumer secret>,
<user ID>");

but since you won't have the user's ID this won't work.

The suggested way of doing this is to use either a security token:
http://code.google.com/apis/friendconnect/opensocial_rest_rpc.html#gadget-token
or the fcauth token: http://code.google.com/apis/friendconnect/opensocial_rest_rpc.html#site-cookie
both of which provide a user context for requests, so "@me" stuff
works with either of these approaches.

FCAuth is a much better way to perform this kind of request than the
security token, but it's not supported directly in the PHP client
right now. Security tokens are possible in the PHP client though:
http://code.google.com/p/opensocial-php-client/wiki/HowToConnecting#Security_Token

I've been meaning to put FCAuth into the PHP client for a little while
now - I think I'll try to do so today. I'll let you know if I get it
working.

Cheers,
~Arne
> community:http://stackoverflow.com/questions/955895/google-friend-connect-opens...)

timborden

unread,
Jun 5, 2009, 11:45:29 AM6/5/09
to opensocial-client-libraries
Arne,

Thanks for the quick response.

I'm not sure I understand how to use the security token. I see the
security token in the javascript:

google.friendconnect.container.initOpenSocialApi({
site: 'XXXXXXXXXXXXXXX',
onload: function(securityToken) { }
});

...but I don't know where I'd get it from in php client.

It's not urgent to get this implemented...it can certainly wait until
next week. I'll watch this space to see how you progress with the
FCAuth approach.

Thanks again for your help,
T


On Jun 5, 11:24 am, Arne Roomann-Kurrik <api.kur...@google.com> wrote:
> Hi Tim,
>
> 2 legged OAuth is used to make requests on behalf of the site and not
> a specific user.  When you authenticate in this way, there's no means
> to determine the currently logged in user, so any requests that need a
> user's context (anything that uses @me, for example) won't work.
>
> The PHP client supports explicitly setting a user context for 2-legged
> by passing an additional parameter:
> $auth = new osapiOAuth2Legged("<consumer key>", "<consumer secret>,
> <user ID>");
>
> but since you won't have the user's ID this won't work.
>
> The suggested way of doing this is to use either a security token:http://code.google.com/apis/friendconnect/opensocial_rest_rpc.html#ga...
> or the fcauth token:http://code.google.com/apis/friendconnect/opensocial_rest_rpc.html#si...
> both of which provide a user context for requests, so "@me" stuff
> works with either of these approaches.
>
> FCAuth is a much better way to perform this kind of request than the
> security token, but it's not supported directly in the PHP client
> right now.  Security tokens are possible in the PHP client though:http://code.google.com/p/opensocial-php-client/wiki/HowToConnecting#S...

Arne Roomann-Kurrik

unread,
Jun 5, 2009, 11:56:36 AM6/5/09
to opensocial-client-libraries
Hi Tim,

You would take the value of securityToken and pass it via Ajax or a
page redirect back to your PHP script, but that's a bit convoluted.
The fcauth token method is a lot better because it's automatically
passed as a cookie to your server.

I was able to implement a quick fcauth class for the client - I'd
appreciate it if you tried it out! You'll need the latest version of
the client library from SVN (revision 155 or later). I've also
written up some documentation about how to use this class:
http://code.google.com/p/opensocial-php-client/wiki/HowToConnecting#FCAuth_Token

Hope this helps,
~Arne

timborden

unread,
Jun 5, 2009, 12:09:49 PM6/5/09
to opensocial-client-libraries
Arne,

Fantastic!

With the 3 new files and the following code, I was able to get the
opensocial id:

$gfc_provider = new osapiFriendConnectProvider();
$gfc_auth = new osapiFCAuth($_COOKIE["fcauth" . GFCSITEID]);
$gfc_osapi = new osapi($gfc_provider, $gfc_auth);
$batch = $this->gfc_osapi->newBatch();
$batch->add($gfc_osapi->people->get(array('userId' => '@me')));
$result = $batch->execute();

Thanks again for your help....I'm really impressed with the
responsiveness!

Cheers,
T

On Jun 5, 11:56 am, Arne Roomann-Kurrik <api.kur...@google.com> wrote:
> Hi Tim,
>
>   You would take the value of securityToken and pass it via Ajax or a
> page redirect back to your PHP script, but that's a bit convoluted.
> The fcauth token method is a lot better because it's automatically
> passed as a cookie to your server.
>
>   I was able to implement a quick fcauth class for the client - I'd
> appreciate it if you tried it out!  You'll need the latest version of
> the client library from SVN (revision 155 or later).  I've also
> written up some documentation about how to use this class:http://code.google.com/p/opensocial-php-client/wiki/HowToConnecting#F...

Arne Roomann-Kurrik

unread,
Jun 5, 2009, 12:35:19 PM6/5/09
to opensocial-cl...@googlegroups.com
Excellent!  I'll work on getting an updated package release pushed out.

~Arne
Reply all
Reply to author
Forward
0 new messages