Quick start for Java

This quick-start app is built in Java and lets you get started with the Google+ platform in a few minutes. The app demonstrates:

  • Using the Google+ Sign-In button to get an OAuth 2.0 refresh token.
  • Exchanging the refresh token for an access token.
  • Making Google+ API requests with the access token, including getting a list of people that the user has circled.
  • Disconnecting the app from the user's Google account and revoking tokens.

System requirements

Step 1: Enable the Google+ API

Create a Google Developers Console project, OAuth 2.0 client ID, and register your JavaScript origins:

  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. In the sidebar, select Credentials.
  6. 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:4567
      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.
  7. 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.

Step 2: Set up the Java quick-start app

  1. Get the latest version of the quick-start. One way is to use git to clone the application repository.

    git clone https://github.com/googleplus/gplus-quickstart-java.git
    

    Or, download the application as a zip file, and extract the library and sample code:

    wget https://github.com/googleplus/gplus-quickstart-java/archive/master.zip
    unzip gplus-quickstart-java-master.zip
    
  2. Change into the sample app directory

    cd gplus-quickstart-java
    
  3. Copy client_secrets.json.example to client_secrets.json.

  4. Edit client_secrets.json, and replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with the values that you generated in Step 1.

Step 3: Run the application

After you have set up your Google Developers Console project and configured the sample app, the app is ready to run.

Because you registered http://localhost:4567 as an Authorized JavaScript origin in the Google Developers Console (in step 1), you will run the sample from that location.

  1. Compile the Java quick-start app:

    mkdir bin
    javac -sourcepath src -d bin -classpath "lib/*" src/com/google/plus/samples/quickstart/Signin.java
    
  2. Run the compiled quick-start app:

    java -classpath "bin:lib/*" com.google.plus.samples.quickstart.Signin
    
  3. Browse to your quick-start app, which by default is at http://localhost:4567.

After you sign in, the application uses the JavaScript client to retrieve your profile and uses the Java client library to retrieve the people visible to the app.

Clicking Disconnect from Google+ results in the server making a call to Google to revoke the refresh token. The server then removes any data that it acquired from Google about the user to disconnect the app and follow the developer policies.

Next steps

Now that you have learned to create a Developers Console project and run a sample app, you can learn more about the Google+ features you can add to your app:

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.