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

Quick-start sample app for Android

This quick-start sample application is an Android project that lets you get started with the Google+ platform in about 10 minutes. The app demonstrates using the Google+ Sign-In button, using SDK methods to list people that the user has circled, and making requests to other Google APIs.

The Google+ platform for Android has the following requirements:

  • A physical device to use for developing and testing because Google Play services can only be installed on an emulator with an AVD that runs Google APIs platform based on Android 4.2.2 or higher.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager.
  • Your project to compile against Android 2.3 (Gingerbread) or higher.
  • The latest version of Android Studio.

Step 1: Enable the Google+ API

To authenticate and communicate with the Google+ APIs, you must create a Google Developers Console project, enable the Google+ API, create an OAuth 2.0 Client ID, and register your digitally signed .apk file's public certificate:

  1. Go to the Google Developers Console .

    Note: Create a single project for the Android, iOS and web versions of your app.

  2. Click Create Project:
    1. In the Project name field, type in a name for your project, such as Quickstart App.
    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. Once the project is created, the name you gave it appears at the top of the left sidebar.
  4. In the left sidebar, select APIs & auth (the APIs sub-item is automatically selected).
    1. Find the Google+ API and set its status to ON—notice that this action moves Google+ API to the top of the list; you can scroll up to see it.
  5. In the sidebar under "APIs & auth", select Consent screen.
    1. Choose an Email Address and specify a Product Name.
  6. In the left sidebar under "APIs & auth", select Credentials.
    1. Click Create a new Client ID—the Create Client ID dialog box appears, as shown further below.
    2. Select Installed application for the application type.
    3. Select Android as the installed application type.
    4. Copy and paste the following package name for this quick-start app into the Package name field:
      com.google.android.gms.plus.sample.quickstart
    5. In a terminal, run the the Keytool utility to get the SHA-1 fingerprint of the certificate. For the debug.keystore, the password is android.
      keytool -exportcert -alias androiddebugkey -keystore <path-to-debug-or-production-keystore> -list -v

      Keytool prints the fingerprint hash to the shell. For example:

      $ keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v
      Enter keystore password: Type "android" if using debug.keystore
      Alias name: androiddebugkey
      Creation date: Aug 27, 2012
      Entry type: PrivateKeyEntry
      Certificate chain length: 1
      Certificate[1]:
      Owner: CN=Android Debug, O=Android, C=US
      Issuer: CN=Android Debug, O=Android, C=US
      Serial number: 503bd581
      Valid from: Mon Aug 27 13:16:01 PDT 2012 until: Wed Aug 20 13:16:01 PDT 2042
      Certificate fingerprints:
         MD5:  1B:2B:2D:37:E1:CE:06:8B:A0:F0:73:05:3C:A3:63:DD
         SHA1: D8:AA:43:97:59:EE:C5:95:26:6A:07:EE:1C:37:8E:F4:F0:C8:05:C8
         SHA256: F3:6F:98:51:9A:DF:C3:15:4E:48:4B:0F:91:E3:3C:6A:A0:97:DC:0A:3F:B2:D2:E1:FE:23:57:F5:EB:AC:13:30
         Signature algorithm name: SHA1withRSA
         Version: 3
      

      Copy the SHA1 fingerprint hash from your terminal. The example above highlights where to find it.

    6. Paste the SHA-1 fingerprint hash into the Signing certificate fingerprint field shown below.
    7. To activate interactive posts, enable the Deep Linking option.

      Shows options in the Create Client ID dialog box.

    8. Click the Create client ID button.

You are done enabling the Google+ API for your app.

Step 2: Run the Google+ quick-start sample app

  1. Clone or download the Android Google+ quickstart Git repository.

    git clone https://github.com/googleplus/gplus-quickstart-android.git
    
  2. Launch Android Studio.

  3. Select File > Import Project.
  4. Navigate to the path to your Android quickstart directory and select gplus-quickstart-android.
    Displays the Android Studio import project dialog with the
    quickstart project selected.
  5. Connect your physical device to your machine by USB. Google Play Services requires a physical device and will not function on an emulator for Android platforms prior to 4.2.2.
    Connect your Android device to your computer to run the application on the physical device.
  6. Select Run > Run 'app' to test the quick-start sample app. Choose your device if prompted. The sample app will launch on your physical device.
    The Android sample
   app launches and allows you to try out the Google+ platform mobile features

Step 3: Verify that the project is configured correctly

Now that the quick-start app is running, you can verify that it is configured correctly by following these steps:

  1. In the quick-start app, click Sign in.
  2. If you have more than one account on your device, you should see a dialog to choose which account to use to sign in to the app.
  3. Choose an account and click OK.
  4. An authorization screen should display. If you do not see a screen that appears similar to the following screenshot, then your API console configuration does not match your running app. This is typically due to a mismatch between either the package ID or the SHA1 hash.

If the quick-start app did not run or you had problems, see the troubleshooting section. Otherwise, start integrating Google+ into your Android app.

Optional: Retrieve a Server Auth Code

The steps above demonstrate how to access Google APIs on behalf of a user while they are using your Android application. If you have a web server that needs to access Google APIs on behalf of the user after they have left your application, you will need to enable server-side API access for your app. This quickstart application implements the client-side portion of the server auth code flow, which you can test with your own web server. Ther following steps will configure this quickstart app to perform the server auth code flow with your server.

  1. Create a web client ID for your server application. You should create this client ID in the same API project as the client ID you created for the Android quickstart.
  2. Configure the Android application. In MainActivity.java, replace WEB_CLIENT_ID with the client ID of your web server and replace SERVER_BASE_URL with the public url for your web server (for example, https://myserver.mydomain.com/).
  3. Implement the code exchange flow on your server to exchange the code sent by the Android application for an access token. There are two steps to implementing this properly:
    1. In MainActivity#onCheckServerAuthorization the Android application will send a GET request to the /selectscopes endpoint of your web server. Your server should respond with a 200 OK status and a space- delimited list of required scopes in the body of the response.
    2. In MainActivity#onUploadServerAuthCode the Android application will send a POST request to the /exchangetoken endpoint of your web server with the server auth code as the serverAuthCode parameter of the request body. Your server should exchange the code for an access token and send a 200 OK response if the exchange was successful, or a 500 Server Error response if the code exchange failed.

Next, start the app on your Android device. Before signing in, check the box labeled Request server auth code in order to force the server auth code flow to start during the sign-in process. If you check the box labeled Mock server has refresh token the app will not actually contact your server but will instead mock a 200 OK response so you can see how the flow will appear to the user. Once you have implemented the steps above, un-check the Mock server has refresh token box and the app will perform the auth code flow with your server as described.

Troubleshooting

The authorization dialog did not display when running the quick-start app
  1. Verify that your package name matches the value that you entered in the APIs console. For the sample, the package name should be com.google.android.gms.plus.sample.quickstart.
  2. Verify that your SHA1 hash matches your API console project.
  3. Check that your debug.keystore file has not expired by examining the output of the keytool command. The line that begins "Valid from." If the keystore file is expired, delete the debug.keystore file and rebuild the app to create a new debug.keystore file. Run the keytool command again to generate a new SHA1 hash then create a new client ID with the new SHA1 hash.
  4. If you are seeing ConnectionResult.getErrorCode() = 4 when trying to connect, ensure that the consent screen is configured and saved.

Next steps

Now that you have run the quick-start sample app, use the following to start integrating Google+ features into your own app:

Otherwise, you can proceed to integrate the GoogleApiClient into your Android activity:

Learn more about the Google+ features you can add to your app: