2017-03-17 65 views
0

我想使用谷歌登录认证到我的PHP的职位。网页上有一个谷歌登入按钮(工作),然后各种功能后,从谷歌到我的PHP获得id_token使用JavaScript和PHP与谷歌认证用户

function getAuth() { 
      var id_token = theUser.getAuthResponse().id_token; 
      var oReq = new XMLHttpRequest(); 
      oReq.onload = function() { 
       console.log(this.responseText); 
      } 
      oReq.open("POST", "databaseAccess.php", true); 
      oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
      oReq.send("oc="+id_token); 
     } 

在服务器端:

<?php 

if (isset($_POST["oc"])) { 
    $code = $_POST["oc"]; 
    $client_id = "xxxxxxxxxxx.apps.googleusercontent.com"; 
    $redirect_uri = "http://myDomain/responder.html"; 
    $client_secret = "xxxxxxxx"; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/v2/auth"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
     'response_type' => 'code', 
     'scope' => 'profile', 
     'client_id' => $client_id, 
     'redirect_uri' => $redirect_uri 
    )); 

    $data = curl_exec($ch); 
} 

echo($data); 

?> 

但结果是“1”,我认为我不应该期待。

SO question我也试过:

$ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token"); 
     curl_setopt($ch, CURLOPT_POST, TRUE); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_FAILONERROR, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, array(
      'code' => $code, 
      'client_id' => $client_id, 
      'client_secret' => $client_secret, 
      'redirect_uri' => $redirect_uri, 
      'grant_type' => 'authorization_code' 
     )); 

     $data = curl_exec($ch); 
echo($data) 

但这没有返回值:从谷歌一个斯托尼沉默。按照docs我也可以做一个更简单的

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 

所以在PHP文件,我想:

$data = http_build_query(array(
     'id_token' => $code 
    )); 
    echo("DATA: ".$data); 

    $context = stream_context_create(array(
    'https' => array(
     'method' => 'GET', 
     'header' => 'Content-Type: application/x-www-form-urlencoded', 
     'content' => $data 
    ) 
)); 

// Make POST request 
    $response = file_get_contents('https://www.googleapis.com/oauth2/v2/tokeninfo', false, $context); 
    echo("PHP OUT: ".$response); 

有了这个,我得到的错误:

either access_token, id_token, or token_handle required 

打字进入网络浏览器:

https://www.googleapis.com/oauth2/v2/tokeninfo?id_token=xxx0Cc6 

从网页的javascript有效令牌我收到给我一个合适的结果:

{ 
"issued_to": "8jijijijijijihb.apps.googleusercontent.com", 
"audience": "8jijijijijijij20hb.apps.googleusercontent.com", 
"user_id": "1128jijijijijij38756", 
"expires_in": 2412, 
"email": "[email protected]", 
"verified_email": true 
} 

希望有人能告诉我怎么样,我的PHP我可以接受的结果,就像这样。使用PHP beta api在我的低端服务器上也证明是徒劳的。

回答

0

试试这个:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={$oc}"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 


$data = curl_exec($ch); 
echo($data) 
+0

谢谢,但是我不知道你指的是用'$ oc'什么。 – Todd

+0

创建一个名为:$ oc = $ _POST ['oc']; –