php - get user info via google api client -
i'm recieving token-id
android client.
in first step i'm validating token via following code:
$tokenid = $request->get('token-id'); $google_client = new google_client(); $google_client->setapplicationname("ashojash"); $google_client->setclientid(env("google_key")); $google_client->setclientsecret(env("google_secret")); $google_client->setincludegrantedscopes(true); $google_client->addscope(['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/plus.login']); $credentials = $google_client->verifyidtoken($tokenid); if ($credentials) { $data = $credentials->getattributes(); // validate aud against server client_id return $data['payload']['sub']; // user id } return false;
received data google is:
array:2 [ "envelope" => array:2 [ "alg" => "rs256" "kid" => "23e5872762976b37944c33e4b2602656093ece91" ] "payload" => array:12 [ "iss" => "https://accounts.google.com" "aud" => "346904023124-73h70s03c0dipla8ltcbcd2ko076ft43.apps.googleusercontent.com" "sub" => "111167217866315036918" "email_verified" => true "azp" => "346904023124-lehbs8510nibuq5ci125h8mu6u2ir4q1.apps.googleusercontent.com" "email" => "jhon.f.kenedy777@gmail.com" "iat" => 1452178925 "exp" => 1452182525 "name" => "john f.kenedy" "given_name" => "john" "family_name" => "f.kenedy" "locale" => "en"] ]
after i've validated token-id, how user info?
i'm interested in are:
1-email
2-user basic information
3-google plus info
i've installed socialite too, if possible include answers socialite too.
using google-api-php-client library, once result $google_client->verifyidtoken can access attributes posted in question.
tested with:
define('root_path', $_server['document_root']); require_once (root_path.'/google-api-php-client/src/google_client.php'); $clientid_debug = 'xxxxxxx.apps.googleusercontent.com'; $clientid_release = 'xxxxxxx.apps.googleusercontent.com'; $mtoken = $_post['mtoken']; $build = $_post['build']; $google_client = new google_client(); if($build=='debug'){ $google_client->setclientid($clientid_debug); }else{ $google_client->setclientid($clientid_release); } $google_result = $google_client->verifyidtoken($mtoken); if ($google_result){ $data = $google_result->getattributes(); $tokenuserid = $data['payload']['sub']; $tokenaudience = $data['payload']['aud']; $tokenemail = $data['payload']['email']; //etc.. //send result android client (could use json send attributes) echo($tokenaudience); }
call android using asynctask:
protected string doinbackground(string... params) { try { //always use https prevent man-in-the-middle attacks! url url = new url("https://www.example.com/validatetokenid.php"); map<string, object> httppostparams = new linkedhashmap<>(); if (buildconfig.debug) { //debug build httppostparams.put("build", "debug"); }else{ //release build httppostparams.put("build", "release"); } httppostparams.put("mtoken", mygooglesignedinaccount.getidtoken()); stringbuilder postdata = new stringbuilder(); (map.entry<string, object> param : httppostparams.entryset()) { if (postdata.length() != 0) postdata.append('&'); postdata.append(urlencoder.encode(param.getkey(), "utf-8")); postdata.append('='); postdata.append(urlencoder.encode(string.valueof(param.getvalue()), "utf-8")); } byte[] postdatabytes = postdata.tostring().getbytes("utf-8"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); conn.setrequestproperty("content-length", string.valueof(postdatabytes.length)); conn.setdooutput(true); conn.getoutputstream().write(postdatabytes); reader in = new bufferedreader(new inputstreamreader(conn.getinputstream(), "utf-8")); stringbuilder strb = new stringbuilder(); ( int c = in.read(); c != -1; c = in.read() ) strb.append((char) c); return strb.tostring(); } catch (exception e) { //this.exception = e; log.e("myapp", e.getmessage()); return e.getmessage(); } }
Comments
Post a Comment