3

私はJSの初心者で、Google +サインインについてGoogle開発者で何度もチェックしました。IDなどを取得しました。ボタンが今何をしているのか、Googleのウィンドウが開きますが、ウィンドウには何も表示されずに閉じます。私が欲しいもの:条件などを受け入れると、人々はつながります。そうでない場合は、インデックスに戻ります。成功した場合は、Google メールでユーザーのメールを取得したいと考えています。

私のコードをチェックしてください:

 (JS in head)

 <head>
 <script type="text/javascript">
 function signinCallback(authResult) {
 if (authResult['access_token']) {
  Successfully authorized;
  // Hide the sign-in button now that the user is authorized, for example:
  document.getElementById('signinButton').setAttribute('style', 'display: none');
 } else if (authResult['error']) {
   // There was an error.
   // Possible error codes:
   //   "access_denied" - User denied access to your app
   //   "immediate_failed" - Could not automatially log in the user
   console.log('There was an error: ' + authResult['error']);
   }
  }
  </script>

  <script type="text/javascript">
   function disconnectUser(access_token) {
    var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
    access_token;

  // Perform an asynchronous GET request.
  $.ajax({
  type: 'GET',
  url: revokeUrl,
  async: false,
  contentType: "application/json",
  dataType: 'jsonp',
  success: function(nullResponse) {
  // Do something now that user is disconnected
  // The response is always undefined.
  },
  error: function(e) {
  // Handle the error
  // console.log(e);
  // You could point users to manually disconnect if unsuccessful
  //https://plus.google.com/apps
  }
 });
}
 // Could trigger the disconnect on a button click
 $('#revokeButton').click(disconnectUser);
</script>

</head>

<body>
<span id="signinButton">
 <span
class="g-signin"
data-callback="signinCallback"
data-clientid="id" (I got mine, it is not the problem)
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.login">
 </span>
</span>

<!-- Place this asynchronous JavaScript just before your </body> tag -->
<script type="text/javascript">
  (function() {
   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = 
true;
   po.src = 'https://apis.google.com/js/client:plusone.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
 })();
</script>

</body>

oh i saw i've got a </div> before the </body> maybe it cause problem, i'll try to 
put the </div> before the script, then </body>. 

This is the url of google devs , Google + Sign-in button for the web :  
https://developers.google.com/+/web/signin/

Thanks in advance for help!!
4

1 に答える 1

2

公式の Google ドキュメント

今後の参考のために、Google Developers サイトの公式ドキュメントを次に示しますサイトには Javascript のサンプルもあります。

Javascript の例と JSFiddle

これは、Google 開発者サイトから恥知らずに取得した JavaScript の例 (および同じ例の jsfiddle ) です。(clientId を更新する必要があるため、jsfiddle は機能することに注意してください)

</body>タグの直前に次の非同期 JavaScript を配置します。

  <link href="http://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <script type="text/javascript">
  (function() {
    var po = document.createElement('script');
    po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/client:plusone.js?onload=render';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
  })();

  function render() {
    gapi.signin.render('customBtn', {
      //'callback': 'signinCallback',
      'clientid': '841077041629.apps.googleusercontent.com',
      'cookiepolicy': 'single_host_origin',
      'requestvisibleactions': 'http://schemas.google.com/AddActivity',
      'scope': 'https://www.googleapis.com/auth/plus.login'
    });
  }
  </script>

サンプルに使用されているいくつかのスタイルを次に示します。

  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: #dd4b39;
      color: white;
      width: 165px;
      border-radius: 5px;
      white-space: nowrap;
    }
    #customBtn:hover {
      background: #e74b37;
      cursor: hand;
    }
    span.label {
      font-weight: bold;
    }
    span.icon {
      background: url('/+/images/branding/btn_red_32.png') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 35px;
      height: 35px;
      border-right: #bb3f30 1px solid;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 35px;
      padding-right: 35px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto',arial,sans-serif;
    }
  </style>

コールバックでは、サインインが成功するとgSignInWrapper要素を非表示にします。

  <div id="gSignInWrapper">
    <span class="label">Sign in with:</span>
    <div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>
      <span class="buttonText">Google</span>
    </div>
  </div>

実際に何が起こっているのですか?OAuth。

このボタンは、OAuth 2.0 サインイン フローをトリガーします。OAuth 2.x を理解しておくと役立つ場合があります。ここにウィキペディアの記事があります。

于 2013-10-18T17:31:20.680 に答える