Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 授权google OAuth 2.0在服务器端,如何检查令牌?_Javascript_Php_Google Oauth_Google Api Php Client - Fatal编程技术网

Javascript 授权google OAuth 2.0在服务器端,如何检查令牌?

Javascript 授权google OAuth 2.0在服务器端,如何检查令牌?,javascript,php,google-oauth,google-api-php-client,Javascript,Php,Google Oauth,Google Api Php Client,我无法在服务器端验证用户令牌ID。你在谷歌的指南上看到了吗 在JS中,我获取id令牌并将其发送到服务器 var googleUser=auth2.currentUser.get(), id\u token=googleUser.getAuthResponse().id\u token; var xhr=new XMLHttpRequest(); xhr.open('POST','http://api.site.loc//tokensignin'); setRequestHeader('Cont

我无法在服务器端验证用户令牌ID。你在谷歌的指南上看到了吗

在JS中,我获取id令牌并将其发送到服务器

var googleUser=auth2.currentUser.get(),
id\u token=googleUser.getAuthResponse().id\u token;
var xhr=new XMLHttpRequest();
xhr.open('POST','http://api.site.loc//tokensignin');
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onload=函数(){
log('Signed-in-as:'+xhr.responseText);
};
send('idtoken='+id_token);
我在服务器上接受

$CLIENT_ID='.apps.googleusercontent.com';
$id_token=$formData['idtoken'];
$client=新的Google_客户端(['client_id'=>$client_id]);
$payload=$client->verifyIdToken($id\u token);
如果($有效载荷){
$userid=$payload['sub'];
//如果请求指定了G套件域:
//$domain=$payload['hd'];
}否则{
//无效的ID令牌
}
因此,我得到了这个错误,而不是确认令牌

<b>Fatal error</b>:  Uncaught exception 'LogicException' with message 'id_token must be passed in or set as part of setAccessToken' in \google-api-php-client\src\Google\Client.php:717
致命错误:未捕获的异常“LogicException”,带有消息“id\u令牌必须传入\google api php client\src\google\client.php:717中的setAccessToken或将其设置为setAccessToken的一部分”

该错误是由于
$id\u令牌
为空(null)所致。 因此,如果您有这样的问题,请检查
$id\u令牌是否正确


感谢John Hanley的帮助

您可能没有令牌(空)或令牌错误(有三种不同类型)。真的,谢谢。修复了所有问题,请将答案与解决方案一起发布。