Google身份工具包:JWT验证失败,状态为500,“message”:null

时间:2016-03-21 15:38:07

标签: php curl jwt google-identity-toolkit

根据implementation guide和常识,我想验证发给通过Google Identity Toolkit登录我网站的用户的JWT令牌,以防止伪造和...以防万一

通过cURL(下面的代码)到https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo的POST请求应该足够了,其中包含idToken(字符串),localId(列表)和电子邮件(列表)。我的应用程序使用IDSP发出的tokenId作为本地ID 但是,这就是我得到的:

Error: call to URL https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=MyAPIKey failed with status 500, response { "error": { "code": 500, "message": null } } , curl_error , curl_errno 0
坦率地说,我完全失败了:我的Google-fu只出现了退出并重新登录,但毫不奇怪它还没有解决问题。
进一步关注的是通过相同的依赖方获取用户显示名称和图像的必要性。

代码

function verifytoken($data){
  $url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=MyAPIKey";
  var_dump($data);
  $content = json_encode($data);
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/json"));
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
  $json_response = curl_exec($curl);
  $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  if ( $status != 201 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
  }
  curl_close($curl);
  $response = json_decode($json_response, true);
  var_dump($response);
}
$tok=array('idToken'=>$gitkitUser->getUserId(),'localId'=>array($gitkitUser->getUserId()),'email'=>array($gitkitUser->getEmail()));
verifytoken($tok);

1 个答案:

答案 0 :(得分:0)

要验证Google Identity Toolkit JWT令牌,您无需发出任何HTTP请求。建议使用Google Identity Toolkit libraries之一(Java / Python / Php / Ruby / Go / Nodejs)在本地执行此操作。令牌已包含用户的email / name / photo_url。