谷歌+;javascript/php登录api
你能帮我怎么用G+Javascript和PHP登录吗? Javascript工作正常,但当我想在服务器上验证令牌时,它会抛出一个异常。我按照这一页去做:但是只有Java/Python。 脚本代码:谷歌+;javascript/php登录api,javascript,php,google-plus,google-login,Javascript,Php,Google Plus,Google Login,你能帮我怎么用G+Javascript和PHP登录吗? Javascript工作正常,但当我想在服务器上验证令牌时,它会抛出一个异常。我按照这一页去做:但是只有Java/Python。 脚本代码: function onSignIn(googleUser) { var id_token = googleUser.getAuthResponse().id_token, profile = googleUser.getBasicProfile(); console.
function onSignIn(googleUser)
{
var id_token = googleUser.getAuthResponse().id_token,
profile = googleUser.getBasicProfile();
console.log(id_token);
$.ajax({
url : "{link :Signgoogle:in}",
accepts : 'json',
type : 'post',
data : {
'token' : id_token
},
...
PHP代码:
public function actionIn()
{
$token = $_POST['token'];
$client = new \Google_Client();
$client->setScopes('email');
$client->setApplicationName(self::APP_NAME);
$client->setDeveloperKey(self::SERVER_KEY);
$client->setAccessToken($token); // This throws an exception
$data = $client->verifyIdToken($token)->getAttributes();
...
$client->setAccessToken
引发异常:无法对令牌进行json解码。你能告诉我如何验证用户登录吗?
非常感谢。我也遇到了同样的问题,我为您的问题找到了以下解决方案:
auth2.attachClickHandler(element, {},
function(googleUser) {
var link = server+"user/google-plus-login"
console.log(link)
console.log(googleUser)
var Name = googleUser['wc']['Ld']
var mail = googleUser['wc']['wc']
var image = googleUser['wc']['zt']
var access_token = googleUser['Ka']['access_token']
}, function(error) {
alert(JSON.stringify(error, undefined, 2));
});
希望这对你也有用 要获取客户端详细信息,只需将令牌传递到此api
if (isset ( $_REQUEST['token'] ) && $_REQUEST['token'] != '') {
$response = file_get_contents ( 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $_REQUEST['token'] );
$response = json_decode ( $response );
echo "<pre>";
print_r ( $response );
echo "</pre>";
}
if(isset($\u请求['token'])&&&$\u请求['token']!=''){
$response=file\u get\u contents($response)https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=“.$”请求[“令牌]);
$response=json_decode($response);
回声“;
打印(回复);
回声“;
}
这里举一个例子:原因是从ajax传递id\u令牌
,在php中,您在setAccessToken($token)
中验证access\u令牌
。
因此,要验证和设置访问令牌,您必须发送access\u-token
,如下所示:
public function actionIn()
{
$token = $_POST['token'];
$access_token = $_POST['access_token'];
$client = new \Google_Client();
$client->setScopes('email');
$client->setApplicationName(self::APP_NAME);
$client->setDeveloperKey(self::SERVER_KEY);
$client->setAccessToken($access_token); // This will not throw exception
$data = $client->verifyIdToken($token)->getAttributes();
...
在后端检查中,如下所示
现在我将Javascript中的id_标记更改为:var jsResult=JSON.stringify(googleUser.getAuthResponse())
并将其发送到PHP,但$client->setAccessToken($jsResponse)抛出无效的标记格式异常。格式看起来像setAccessToken()所需。当然$client->setAccessToken()会引发异常,因为它需要access\u令牌,但我只有js API中的id\u令牌。看来我应该只使用$client->verifyIdToken()。但它让我死定了。有点不对劲,我不知道是什么。好吧,我找到了另一个流程,你可以在这里找到:
public function actionIn()
{
$token = $_POST['token'];
$access_token = $_POST['access_token'];
$client = new \Google_Client();
$client->setScopes('email');
$client->setApplicationName(self::APP_NAME);
$client->setDeveloperKey(self::SERVER_KEY);
$client->setAccessToken($access_token); // This will not throw exception
$data = $client->verifyIdToken($token)->getAttributes();
...