Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- Copyright (c) 2011 Google Inc.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not
- use this file except in compliance with the License. You may obtain a copy of
- the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- License for the specific language governing permissions and limitations under
- the License.
- To run this sample, set apiKey to your application's API key and clientId to
- your application's OAuth 2.0 client ID. They can be generated at:
- https://console.developers.google.com/apis/credentials?project=_
- Then, add a JavaScript origin to the client that corresponds to the domain
- where you will be running the script. Finally, activate the People API at:
- https://console.developers.google.com/apis/library?project=_
- -->
- <!DOCTYPE html>
- <html>
- <head>
- <title>Say hello using the People API</title>
- <meta charset='utf-8' />
- <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
- </head>
- <body>
- <!--Add buttons to initiate auth sequence and sign out-->
- <button id="authorize-button" style="display: none;">Authorize</button>
- <button id="signout-button" style="display: none;">Sign Out</button>
- <div id="content"></div>
- <script type="text/javascript">
- var apiKey = 'xx';
- var clientId = 'xx-xx.apps.googleusercontent.com';
- var scopes = 'openid';
- var authorizeButton = document.getElementById('authorize-button');
- var signoutButton = document.getElementById('signout-button');
- function handleClientLoad() {
- gapi.load('client:auth2', initClient);
- }
- function initClient() {
- gapi.client.init({
- apiKey: apiKey,
- clientId: clientId,
- scope: scopes
- }).then(function () {
- // Listen for sign-in state changes.
- gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
- // Handle the initial sign-in state.
- updateSigninStatus(gapi.auth2);
- authorizeButton.onclick = handleAuthClick;
- signoutButton.onclick = handleSignoutClick;
- });
- }
- function updateSigninStatus(data) {
- var isSignedIn = data.getAuthInstance().isSignedIn.get();
- if (isSignedIn) {
- authorizeButton.style.display = 'none';
- signoutButton.style.display = 'block';
- makeApiCall(data);
- } else {
- authorizeButton.style.display = 'block';
- signoutButton.style.display = 'none';
- }
- }
- function handleAuthClick(event) {
- gapi.auth2.getAuthInstance().signIn();
- }
- function handleSignoutClick(event) {
- gapi.auth2.getAuthInstance().signOut();
- }
- // Load the API and make an API call. Display the results on the screen.
- function makeApiCall(data) {
- var user = data.getAuthInstance().currentUser.get();
- user.grantOfflineAccess({
- }).then(function (resp) {
- $('#content').append('<br>code: ' + resp.code);
- });
- }
- </script>
- <script async defer src="https://apis.google.com/js/api.js"
- onload="this.onload=function(){};handleClientLoad()"
- onreadystatechange="if (this.readyState === 'complete') this.onload()">
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment