Forgive me if I missed something, but how do you know which Google account to link them to if you do not have your email address? Typically, you prompt the user to enter their Google email address to find their profile first.
Using OAuth2, you can request permissions through the scope parameter. ( Documentation. ) I assume that the areas you want are https://www.googleapis.com/auth/userinfo.email and https://www.googleapis.com/auth/userinfo.profile .
Then, if you received an access token, just get profile information . (I assume that you were able to redeem the return authorization code for the access token?) Just make a request for receipt to https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} , which returns an array of JSON profile data including email:
{ "id": "00000000000000", "email": " fred.example@gmail.com ", "verified_email": true, "name": "Fred Example", "given_name": "Fred", "family_name": "Example", "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg", "gender": "male", "locale": "en-US" }
There must be a method in the PHP library to execute this request, but I cannot find it. There are no guarantees, but try the following:
$url = "https://www.googleapis.com/oauth2/v1/userinfo"; $request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET'))); if ((int)$request->getResponseHttpCode() == 200) { $response = $request->getResponseBody(); $decodedResponse = json_decode($response, true); //process user info } else { $response = $request->getResponseBody(); $decodedResponse = json_decode($response, true); if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) { $response = $decodedResponse['error']; } } }
In any case, in the code you posted just pass the required scope to createAuthUrl() :
else { $authUrl = $client->createAuthUrl("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"); print "<a class='login' href='$authUrl'>Connect Me!</a>"; }