2016-06-18 6 views
-1

コンポーザーをインストールしないで、のGoogleログイン認証で1週間苦労しています。質問/回答形式に従うために答えが続きます。phpバックエンドのための簡単なGoogleのサインインID

+1

本当にここに質問はありません。あなた自身の質問に答えることが奨励されます。この質問を編集して質問にし、回答を投稿する必要があります。将来の読者には役立つかもしれないが、それはこのサイトのフォーマットに違反し、トピックから外れている –

答えて

1

最終結果はシンプルですが、私は多くの間違った終了を経験しました。私は他の人のためにそれを共有します。私はここのツールの専門家ではないので、改善は大歓迎です。

まず、ユーザーに投稿されたHTML5:何らかの理由で

<!DOCTYPE html> 
<html lang="en"> 

    <head> 

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

    <title>demonstration of google sign-in for php</title> 


    <!-- you must add your own value here --> 
    <meta name="google-signin-client_id" content="<yourletterstring>.apps.googleusercontent.com"> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"> </script> 

    <script src="https://apis.google.com/js/platform.js" async defer></script> 

    <script> 
     function onSignIn(googleUser) { 
     var profile = googleUser.getBasicProfile(); 
     console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead. 
     console.log('Name: ' + profile.getName()); 
     console.log('Image URL: ' + profile.getImageUrl()); 
     console.log('Email: ' + profile.getEmail()); 
     $('#gid').html(profile.getId()); 
     $('#gemail').html(profile.getEmail()); 
     $('#gimg').attr("src", profile.getImageUrl()); 
     $('#gname').html(profile.getName()); 
     $('#gstatus').html("logged in"); 
     $('#gstatus').attr("style", "color:green"); 

     var id_token = googleUser.getAuthResponse().id_token; 
     $('#gtoken').html(id_token); 

     clickurl = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='+id_token; 
     $('#gtokenurl').html('<a href="'+clickurl+'">click here</a>'); 

     $('#fidtoken').attr("value", id_token); // passing into form 

     $('#showresults').attr("display", "block"); // not working. 
     } 


     function signOut() { 
     var auth2 = gapi.auth2.getAuthInstance(); 
     auth2.signOut().then(function() { 
      console.log('User signed out.'); 
      $('#gid').html(""); 
      $('#gemail').html(""); 
      $('#gimg').removeAttr("src"); 
      $('#gname').html(""); 
      $('#gstatus').html("logged out"); 
      $('#gstatus').attr("style", "color:red"); 

      $('#gtoken').html(""); 
      $('#gtokenurl').html(""); 

      $('#showresults').attr("display", "none"); // not working 
      $('#showresults').attr("visibility", "hidden"); 
     }); 
     } 

    </script> 

    <style>td { padding:1ex; }</style> 

    </head> 

    <body style="margin:3em; background-color:khaki"> 

    <h1> demonstration of google sign-in for php </h1> 

    <p> This is a working example of google sign in for a php website. Start reading <a href="https://developers.google.com/identit 
y/sign-in/web/devconsole-project">Google Sign-in For Websites</a>. In particular, link in your developers console.</p> 

    <p> In this javascript code, you only ever change one value above: 
     <pre> 
    &lt;meta name="google-signin-client_id" content="&lt;your value here&gt;.apps.googleusercontent.com"&gt; 
     </pre> 



    <h2> login button </h2> 

    <div class="g-signin2" data-onsuccess="onSignIn"></div> 
    <a href="#" onclick="signOut();">Sign out</a> 


    <div id="showresults"> <!-- not working suppression --> 

     <h2> results in javascript, not secure from server perspective </h2> 

     <form method="POST" action="googlereturn.php"> 
    <input id="fidtoken" type="hidden" name="idtoken" value="" /> 
    <input type="submit" value="Pass Results To PHP" style="font-size:xx-large;margin:1ex;" /> 
     </form> 

     <table id="showresults"> 
    <tr> <td>ID: </td> <td id="gid"></td> </tr> 
    <tr> <td>Name: </td> <td id="gname"></td> </tr> 
    <tr> <td>Img: </td> <td> <img id="gimg" /> </td> </tr> 
    <tr> <td>Email: </td> <td id="gemail"> </td> </tr> 
    <!-- <tr> <td>Token: </td> <td id="gtoken"> </td> </tr> --> 
    <tr> <td>Token URL: </td> <td id="gtokenurl"> </td> </tr> 
    <tr> <td>Status: </td> <td id="gstatus"> </td> </tr> 
     </table> 

    </div> 

    </body> 
</html> 

、ログアウトしたとき、私は存在のうちのhtml結果を点滅させることはできませんが、これは無視することができます美しさの傷です。

受信者のPHP googlereturn.phpスクリプトが今だ

<html> 
    <head> 
     <title> google php identification recipient </title> 
    </head> 

    <body style="margin:3em; background-color:khaki"> 

     <h1> google php identification recipient </h1> 

     <?php 

     echo "<pre> "; print_r($_POST); echo "</pre>"; 

     $checkendpoint= "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token="; 
     $checkurl= $checkendpoint.$_POST['idtoken']; 

     echo "<h2> google identity </h2>\n"; 

     $v=file_get_contents($checkurl); 

     echo "<pre>$v</pre>\n"; 

     ?> 

    <p>(This identity information was sent directly from <?= $checkendpoint ?> and thus can be trusted.)</p> 
</body> 
</html> 

です。これは壁に頭を打つ1週間に関連していると信じるのは難しいです。

他人に役立つことを願っています。

/iaw

関連する問題