Click here to see your recently viewed pages and most viewed pages.
Hide
Google+ Platform

Getting people and profile information

After you have signed in a user with Google, you can access the user's age range, language, public profile information, and people that they have circled. If you request the plus.profile.emails.read scope, you can also get their email address. With this rich social data, you can build engaging experiences and an instant community in your app. For example, you might connect your user with their friends that also use your app or you might make suggestions based on their friends' activities within your app.

Before you begin

You must create a Google Developers Console project, create an OAuth 2.0 client ID, and register your JavaScript origins and redirect URIs:

  1. Go to the Google Developers Console .
  2. Select a project, or create a new one by clicking Create Project:

    Note: Use a single project to hold all platform instances of your app (Android, iOS, web, etc.), each with a different Client ID.

    1. In the Project name field, type in a name for your project.
    2. In the Project ID field, optionally type in a project ID for your project or use the one that the console has created for you. This ID must be unique world-wide.
    3. Click the Create button and wait for the project to be created. Note: There may be short delay of up to 30 seconds before the project is created. The project name appears at the top of the left sidebar, indicating you are now editing the project.
  3. In the left sidebar, select APIs & auth. A list of Google web services appears.
  4. Find the Google+ API service and set its status to ON—notice that this action moves the service to the top of the list.
  5. Enable any other APIs that your app requires.
  6. In the sidebar, select Credentials.
  7. In the OAuth section of the page, select Create New Client ID.
    In the resulting Create Client ID dialog box, register the origins where your app is allowed to access the Google APIs, as follows. The origin is the unique combination of protocol, hostname, and port.
    1. In the Application type section of the dialog, select Web application.
    2. In the Authorized JavaScript origins field, enter the origin for your app. You can enter multiple origins to allow for your app to run on different protocols, domains, or subdomains. Wildcards are not allowed. In the example below, the second URL could be a production URL.
      http://localhost:8080
      https://myproductionurl.example.com
      
    3. In the Authorized redirect URI field, delete the default value. It is not used for this case.
    4. Select Create Client ID.
  8. In the resulting Client ID for web application section, note or copy the Client ID and Client secret that your app will need to use to access the APIs.

Retrieve a collection of people

Use the people.list API method to get a list of people in the specified collection. The response contains a list of people objects that the user chose to share with your app. This list does not include the names of circles.

Using the visible collection, you can obtain a list of people who this user has added to one or more circles, limited to the circles visible to the requesting app.

JavaScript example:

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/javascript
gapi.client.load('plus','v1', function(){
 var request = gapi.client.plus.people.list({
   'userId': 'me',
   'collection': 'visible'
 });
 request.execute(function(resp) {
   console.log('Num people visible:' + resp.totalItems);
 });
});

The people.list API method contains additional examples in other programming languages.

Retrieve profile information

To retrieve profile information for a user, use the people.get API method. To get profile information for the currently authorized user, use the userId value of me.

JavaScript example:

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/javascript
gapi.client.load('plus','v1', function(){
 var request = gapi.client.plus.people.get({
   'userId': 'me'
 });
 request.execute(function(resp) {
   console.log('Retrieved profile for:' + resp.displayName);
 });
});

The people.get API method contains additional examples in other programming languages.

Retrieve an authenticated user's email address

You can get an email address for the authenticated user by using the email scope.

The following code JavaScript code example demonstrates how to:

  1. Use Google+ Sign-In to authenticate the user and get a valid OAuth 2.0 access token.
  2. Use the token to make a HTTP GET request to the https://www.googleapis.com/plus/v1/people/me REST end point.
  3. Parse the response and display the user's email address.

Try it:

Code example: If you save this example as a local file, you should give it a ".jshtml" file extension, such as "signin_email_demo.jshtml".

<html>

  <head>
    <title>Demo: Getting an email address using the Google+ Sign-in button</title>
    <!-- Include the API client and Google+ client. -->
    <script src = "https://plus.google.com/js/client:plusone.js"></script>
  </head>

  <body>
    <!-- Container with the Sign-In button. -->
    <div id="gConnect" class="button">
      <button class="g-signin"
          data-scope="email"
          data-clientid="841077041629.apps.googleusercontent.com"
          data-callback="onSignInCallback"
          data-theme="dark"
          data-cookiepolicy="single_host_origin">
      </button>
      <!-- Textarea for outputting data -->
      <div id="response" class="hide">
        <textarea id="responseContainer" style="width:100%; height:150px"></textarea>
      </div>
    </div>
 </body>

  <script type="text/javascript">
  /**
   * Handler for the signin callback triggered after the user selects an account.
   */
  function onSignInCallback(resp) {
    gapi.client.load('plus', 'v1', apiClientLoaded);
  }

  /**
   * Sets up an API call after the Google API client loads.
   */
  function apiClientLoaded() {
    gapi.client.plus.people.get({userId: 'me'}).execute(handleEmailResponse);
  }

  /**
   * Response callback for when the API client receives a response.
   *
   * @param resp The API response object with the user email and profile information.
   */
  function handleEmailResponse(resp) {
    var primaryEmail;
    for (var i=0; i < resp.emails.length; i++) {
      if (resp.emails[i].type === 'account') primaryEmail = resp.emails[i].value;
    }
    document.getElementById('responseContainer').value = 'Primary email: ' +
        primaryEmail + '\n\nFull Response:\n' + JSON.stringify(resp);
  }

  </script>

</html>