AndreaCi

Google OAuth - 2

Jan 9th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.53 KB | None | 0 0
  1. <!--
  2.  Copyright (c) 2011 Google Inc.
  3.  
  4.  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5.  use this file except in compliance with the License. You may obtain a copy of
  6.  the License at
  7.  
  8.  http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.  Unless required by applicable law or agreed to in writing, software
  11.  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12.  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13.  License for the specific language governing permissions and limitations under
  14.  the License.
  15.  
  16.  To run this sample, set apiKey to your application's API key and clientId to
  17.  your application's OAuth 2.0 client ID. They can be generated at:
  18.    https://console.developers.google.com/apis/credentials?project=_
  19.  Then, add a JavaScript origin to the client that corresponds to the domain
  20.  where you will be running the script. Finally, activate the People API at:
  21.    https://console.developers.google.com/apis/library?project=_
  22. -->
  23. <!DOCTYPE html>
  24. <html>
  25.   <head>
  26.     <title>Say hello using the People API</title>
  27.     <meta charset='utf-8' />
  28.   <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
  29.   </head>
  30.   <body>
  31.  
  32.     <!--Add buttons to initiate auth sequence and sign out-->
  33.     <button id="authorize-button" style="display: none;">Authorize</button>
  34.     <button id="signout-button" style="display: none;">Sign Out</button>
  35.  
  36.     <div id="content"></div>
  37.  
  38.     <script type="text/javascript">
  39.       var apiKey = 'xx';
  40.       var clientId = 'xx-xx.apps.googleusercontent.com';
  41.       var scopes = 'openid';
  42.  
  43.  
  44.       var authorizeButton = document.getElementById('authorize-button');
  45.       var signoutButton = document.getElementById('signout-button');
  46.  
  47.       function handleClientLoad() {
  48.         gapi.load('client:auth2', initClient);
  49.       }
  50.  
  51.       function initClient() {
  52.         gapi.client.init({
  53.             apiKey: apiKey,
  54.             clientId: clientId,
  55.             scope: scopes
  56.         }).then(function () {
  57.           // Listen for sign-in state changes.
  58.           gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
  59.  
  60.           // Handle the initial sign-in state.
  61.           updateSigninStatus(gapi.auth2);
  62.  
  63.           authorizeButton.onclick = handleAuthClick;
  64.           signoutButton.onclick = handleSignoutClick;
  65.         });
  66.       }
  67.  
  68.       function updateSigninStatus(data) {
  69.         var isSignedIn = data.getAuthInstance().isSignedIn.get();
  70.         if (isSignedIn) {
  71.           authorizeButton.style.display = 'none';
  72.           signoutButton.style.display = 'block';
  73.           makeApiCall(data);
  74.         } else {
  75.           authorizeButton.style.display = 'block';
  76.           signoutButton.style.display = 'none';
  77.         }
  78.       }
  79.  
  80.       function handleAuthClick(event) {
  81.         gapi.auth2.getAuthInstance().signIn();
  82.       }
  83.  
  84.       function handleSignoutClick(event) {
  85.         gapi.auth2.getAuthInstance().signOut();
  86.       }
  87.  
  88.       // Load the API and make an API call.  Display the results on the screen.
  89.       function makeApiCall(data) {
  90.       var user =  data.getAuthInstance().currentUser.get();
  91.                  
  92.       user.grantOfflineAccess({
  93.             }).then(function (resp) {
  94.                   $('#content').append('<br>code: ' + resp.code);
  95.             });
  96.      
  97.       }
  98.     </script>
  99.     <script async defer src="https://apis.google.com/js/api.js"
  100.      onload="this.onload=function(){};handleClientLoad()"
  101.      onreadystatechange="if (this.readyState === 'complete') this.onload()">
  102.     </script>
  103.   </body>
  104. </html>
Add Comment
Please, Sign In to add comment