你能告诉我,我怎样才能通过Google+ Javascript/PHP的API获取用户数据(姓名,电子邮件)?我按照这个tutorial去,它描述了如何通过Javascript获得authResult['code']
。通过Javascript和PHP登录Google+
这就是我确定。我通过Ajax将它发送到服务器,但我不知道如何通过PHP BUT获取用户数据,如姓名和电子邮件,而无需任何重定向。下面是代码 的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检索一个access_token
但没有用户数据。当然,这是一个Ajax调用,所以我不能像Google在每个页面上描述的那样执行任何重定向。你能帮我吗,我找不到任何解决方案。
非常感谢。
首先谢谢。但是,$加上变量从哪里来?这是什么?我不是女巫。 –
我发现这个代码:$ plus = new \ Google_PlusService($ client);但它会抛出异常:未找到类“Google_PlusService”。我已经通过Composer安装Google API:require:{“google/apiclient”:“1.0.*@beta”} –
这是疯了!看来正确的形式是$ plus = new \ Google_Service_Plus($ client); –