php - How to set up Google+ Sign-In for server-side apps for a website? -
i trying add together sign in google+ button on website retrieve basic information. documentation doesnt seem create sense me. (https://developers.google.com/+/web/signin/server-side-flow) appears out of date , not finish , there seems various api library's can used.
can explain more or tell me how should go making work , api library utilize etc? total sample code helpful.
thanx
ok add together more detail. google development page gives illustration login button :
<html> <head> <!-- begin pre-requisites --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script> <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer> </script> <!-- end pre-requisites --> </head> <body> <div id="signinbutton"> <span class="g-signin" data-scope="https://www.googleapis.com/auth/plus.login" data-clientid="your-client-id" data-redirecturi="postmessage" data-accesstype="offline" data-cookiepolicy="single_host_origin" data-callback="signincallback"> </span> </div> <div id="result"></div> <script> function signincallback(authresult) { if (authresult['code']) { // hide sign-in button user authorized, example: $('#signinbutton').attr('style', 'display: none'); // send code server $.ajax({ type: 'post', url: 'plus.php?storetoken', contenttype: 'application/octet-stream; charset=utf-8', success: function(result) { // handle or verify server response if necessary. // prints list of people user has allowed app know // console. console.log(result); if (result['profile'] && result['people']){ $('#results').html('hello ' + result['profile']['displayname'] + '. made server side phone call people.get , people.list'); } else { $('#results').html('failed create server-side call. check configuration , console.'); } }, processdata: false, data: authresult['code'] }); } else if (authresult['error']) { // there error. // possible error codes: // "access_denied" - user denied access app // "immediate_failed" - not automatially log in user // console.log('there error: ' + authresult['error']); } } </script> </body> </html>
but provides:
<?php // create state token prevent request forgery. // store in session later validation. $state = md5(rand()); $app['session']->set('state', $state); // set client id, token state, , application name in html while // serving it. homecoming $app['twig']->render('index.html', array( 'client_id' => client_id, 'state' => $state, 'application_name' => application_name )); // ensure no request forgery going on, , user // sending connect request user supposed to. if ($request->get('state') != ($app['session']->get('state'))) { homecoming new response('invalid state parameter', 401); } $code = $request->getcontent(); $gplusid = $request->get['gplus_id']; // exchange oauth 2.0 authorization code user credentials. $client->authenticate($code); $token = json_decode($client->getaccesstoken()); // verify token $requrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' . $token->access_token; $req = new google_httprequest($requrl); $tokeninfo = json_decode( $client::getio()->authenticatedrequest($req)->getresponsebody()); // if there error in token info, abort. if ($tokeninfo->error) { homecoming new response($tokeninfo->error, 500); } // create sure token got intended user. if ($tokeninfo->userid != $gplusid) { homecoming new response( "token's user id doesn't match given user id", 401); } // create sure token got our app. if ($tokeninfo->audience != client_id) { homecoming new response( "token's client id not match app's.", 401); } // store token in session later use. $app['session']->set('token', json_encode($token)); $response = 'succesfully connected token: ' . print_r($token, true); ?>
but doesnt set lastly bit of code or how refer api library or set secret or anything. pointing in righ direction please?
ok if else having trouble. i followed tutorial on link
i downloaded api library there, changed configs file , used illustration provided , worked fine.
to create work on localhost have set authorized javascript origins localhost:# illustration http://localhost:12345
then create browser take folder or signin page in command prompt type in
cd c:/the/path/of/the/downloaded/api/example
then type in:
php -s localhost:12345
hope helps anyone
php google-plus
No comments:
Post a Comment