Find file
6178655 Oct 26, 2016
@kevinthecheung @gregor @chirags @bochunz
122 lines (102 sloc) 4.55 KB
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
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:
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:
<!DOCTYPE html>
<title>Say hello using the People API</title>
<meta charset='utf-8' />
<p>Say hello using the People API.</p>
<!--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">
// Enter an API key from the Google API Console:
var apiKey = 'YOUR_API_KEY';
// Enter the API Discovery Docs that describes the APIs you want to
// access. In this example, we are accessing the People API, so we load
// Discovery Doc found here:
var discoveryDocs = ["$discovery/rest?version=v1"];
// Enter a client ID for a web application from the Google API Console:
// In your API Console project, add a JavaScript origin that corresponds
// to the domain where you will be running the script.
var clientId = '';
// Enter one or more authorization scopes. Refer to the documentation for
// the API or
// for details.
var scopes = 'profile';
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
function handleClientLoad() {
// Load the API client and auth2 library
gapi.load('client:auth2', initClient);
function initClient() {
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes
}).then(function () {
// Listen for sign-in state changes.
// Handle the initial sign-in state.
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
function updateSigninStatus(isSignedIn) {
if (isSignedIn) { = 'none'; = 'block';
} else { = 'block'; = 'none';
function handleAuthClick(event) {
function handleSignoutClick(event) {
// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
resourceName: 'people/me'
}).then(function(resp) {
var p = document.createElement('p');
var name = resp.result.names[0].givenName;
p.appendChild(document.createTextNode('Hello, '+name+'!'));
<script async defer src=""
onreadystatechange="if (this.readyState === 'complete') this.onload()">