Google Plus身份验证 - Ajax令牌

时间:2013-07-12 16:36:43

标签: php google-plus

我正在玩Google Plus API。我想创建一个基本应用程序,使用服务器端进程将用户详细信息存储到我的数据库中:

https://developers.google.com/+/web/signin/server-side-flow

我目前正在进行第6步。我的按钮出现在页面上,我可以使用ajax将code(id)发送到服务器。类似的东西:

var dataString = 'auth= ' +authResult['code'];

// Send the code to the server
$.ajax({
    type: 'POST',
    url: 'storeToken',
    dataType: 'html',
    data: dataString,
    processData: false,
    statusCode: {
        200: function(result){
            $('#test').html(result);
                if (result['profile'] && result['people']){
                    $('#test').html('Hello ' + result['profile']['displayName'] + '. You successfully made a server side call to people.get and people.list');
                } else {
                    $('#test').html('Failed to make a server-side call. Check your configuration and console.');
                }
        }

    },
});

现在在文档中,它说我应该将code转换为access_token和refresh_token - 但这是我似乎无法做到的部分......

我的ajax发布到哪里,我可以使用从上面的脚本成功传递的$ code。我试过了:

$client = new Google_Client();
$client->authenticate($code); 

但是从这里开始,我不知道该怎么做。文档在它可以使用的响应中提到:result['profile']['displayName']但是,我不知道它是如何工作的。

1 个答案:

答案 0 :(得分:0)

您需要做的下一件事是提取访问令牌。这样您就可以代表经过身份验证的用户开始进行API调用。

$token = json_decode($client->getAccessToken());

您还需要验证令牌。要查看完整示例,请查看https://developers.google.com/+/web/signin/server-side-flow#using_one-time-code_flow上的第8步。