Google + Login über Javascript und PHP
Kannst du mir bitte sagen, wie ich Benutzerdaten (Name, E-Mail) über Google+ Javascript / Php api erhalten kann? Ich gehe nach diesemLernprogram, das beschreibt, wie man an @ komauthResult['code']
via Javascript.
Thats ok ich habe es. Ich sende es über Ajax an den Server, aber ich weiß nicht, wie ich Benutzerdaten wie Name und E-Mail über PHP erhalten kann, ABER ohne Weiterleitungen. Hier sind die Beispiele für Code-Javascript:
function googleStart()
{
console.log('googleStart');
gapi.load('auth2', function()
{
auth2 = gapi.auth2.init({
client_id: '811214467813-v3fmui5dfghte5m0kmohsf6dl11ori3tg.apps.googleusercontent.com',
// Scopes to request in addition to 'profile' and 'email'
fetch_basic_profile: false,
scope: 'profile email'
});
});
}
function googleSignIn(authResult)
{
console.log(authResult);
if (authResult['code']) {
console.log(authResult);
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url : "{link :Signgoogle:in}",
accepts : 'json',
// Spýtať sa na DJPW čo to tu je zakomentované
//contentType: 'application/octet-stream; charset=utf-8',
//processData: false,
data : {
'code' : authResult['code'],
'id_token' : authResult.getAuthResponse().id_token
},
...
PHP:
public function actionIn()
{
$code = $_POST['code'];
$client = new \Google_Client();
$client->setClientId(self::APP_ID);
$client->setClientSecret(self::APP_SECRET);
$client->setRedirectUri(self::REDIRECT_URI);
$client->authenticate($code);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
$client->getAuth();
$data = $access_token;
if($data)
{
$this->sendJson(array($data));
}
else
{
$data = array('error' => '$client->verifyIdToken($id) skočil chybou.');
$this->sendJson($data);
}
}
....
PHP ruft ein @ access_token
aber keine Benutzerdaten. Natürlich handelt es sich um einen Ajax-Aufruf, daher kann ich keine Weiterleitungen durchführen, wie von Google auf jeder Seite beschrieben. Können Sie mir bitte helfen, ich kann keine Lösung finden.
Vielen Dank