Google Contacts API in Javascript and PHP -


i making ajax controlled website, in use javascript sdk google authenticate users, , gain access google contacts. intend do, following:

  1. authenticate user in browser, withour redirecting , ask access contact list, , access manage contacts.
  2. store user id in database if he/she granted me access, refresh token, if not mistaking, can via server side.
  3. sometime later, if user wants see google contacts via website, send ajax request server, asks access_token user, retrieves data , shows user, or stores in own database if user asks to.

i've managed complete first step these three, can authenticate user, , access single access token valid 3600 hours, can't figure out how authenticate user server side, without redirecting him anywhere. tried using google php sdk too, can't seem figure out how this. possible somehow, because stated in google php sdk guide:

if have authenticated on android or javascript web client, may have aquired code via different means. in case, need exchange it. if retrieved via gapi javascript client, need set redirect uri postmessage.

  $client->setredirecturi($redirect_uri); 

the problem don't understand how this. $redirect_uri? don't have redirect url, becase when implemented javascript sdk, there said, don't have use redirect uri, because javascript authentification done in same window, without redirects(just want it).

how proceed solve second , third step mentioned above? find non-hacky or not-very-much-hacky tutorial, achieve desired result?

edit:

what want achieve following things:

  • ask permission access google contacts user, without redirecting him site(via popup window)

achieved javascript sdk

  • get access token permission, , refresh token, , store these in database

javascript sdk grants access token, , don't want pass via ajax call, because feel unsecure

  • with refresh token, generate access tokens server side user, , process data, , send data back.

here how php file looks @ moment:

$client = new google_client(); $client->setclientid($client_id); $client->setclientsecret($client_secret); $client->setscopes('https://www.googleapis.com/auth/plus.me'); $client->setredirecturi($php_self); $client->setstate('offline');  $authurl = $client->createauthurl();  if (isset($_get['code'])) {     $client->authenticate($_get['code']);     $_session['access_token'] = $client->getaccesstoken();     $result=$client->getaccesstoken(); } else {     header("location: ".$authurl);     exit; } 

to achieve want, should $_get['code'] parameter somehow through javascript sdk, don't know how:|

after few days of headache figured 1 out too... google nothing, documentation sucks.

 $client->setredirecturi($redirect_uri); 

the $redirect_uri parameter should string: "postmessage", , when authenticating via javascript, should ask code instead of token, send server side script, authenticate , exchange token.

  gapi.auth.authorize({client_id: googleapi.clientid, scope: googleapi.scopes, response_type: 'code', immediate: true}, g_handleauthresult); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -