Using Scribe OAuth Java API with Google OAuth 2 Tutorial

1- Problem of Google Oauth1a

There is one problem of  Google OAuth 1a. From 20 April, 2015, Google will no support for  OAuth 1 protocol any more. If you use  Scribe Java API to work with  Google OAuth 1.0, you must change your code.

In this document, I will instruct you to use  Scribe Java API to work with  Google OAuth 2.0

2- Create Project in Google Developers Console

In the first time, you do not have any Project, so you have to create it. In most cases, you should name Project same your Website name (It is not compulsory).
Project Name: ExampleWebsite
Project has been created:
Goto "APIs and auth/Consent screen", to create a Product Name.
Create Client ID:
In case you use  OAuth2.0 on a web application, you will have to create ClientID for that web application. Parallel, you declare the redirecting link (The link will be redirected to after user logs in his/her google account and allows your application to connect with Google OAuth).
ClientID is created, in which the two most important kinds of information are  Client ID and  Client Secret. You need to have it in Java code (Illustrated in the following example).
‚ÄčNote: You can create multiple ClientID  information for your different applications.

3- Quick Create Project & declare Scribe Java API library

  • File/New/Others
Your Project was created.
Configuring Maven use Scribe Java API:
  • pom.xml
<project xmlns=""


        <!-- -->


4- Code Project

Scribe Java API is an API to help you work more easily with OAuth, it hides the differences between the OAuth service providers (Google, yahoo, facebook, ..), and support OAuth 1a, OAuth 2.0
For example, to work with LinkIn, your code is:
OAuthService service = new ServiceBuilder()
For example, to work with Google OAuth 1a, your code is:
OAuthService service = new ServiceBuilder()
The API(s) is available on Scribe:
  • FacebookApi
  • GoogleApi
  • FoursquareApi
  • Foursquare2Api
  • YahooApi
  • TwitterApi
  • ....
Regrettably, GooleApi is used for Google OAuth 1, not for Google OAuth 2.0. You also cannot find Google2Api class in the library of Scribe 1.3.7 (Maybe it is due to copyright problem).

Hence, in your project, you need to create Google2Api class, and note that it must be contained in org.scribe.builder.api package:
package org.scribe.builder.api;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.OAuthConfig;
import org.scribe.model.OAuthConstants;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuth20ServiceImpl;
import org.scribe.oauth.OAuthService;
import org.scribe.utils.OAuthEncoder;
import org.scribe.utils.Preconditions;

 * Google OAuth2.0 Released under the same license as scribe (MIT License)
 * @author yincrash
public class Google2Api extends DefaultApi20 {

    private static final String AUTHORIZE_URL = "";
    private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL
            + "&scope=%s";

    public String getAccessTokenEndpoint() {
        return "";

    public AccessTokenExtractor getAccessTokenExtractor() {
        return new AccessTokenExtractor() {

            public Token extract(String response) {
                                "Response body is incorrect. Can't extract a token from an empty string");

                Matcher matcher = Pattern.compile(
                        "\"access_token\" : \"([^&\"]+)\"").matcher(response);
                if (matcher.find()) {
                    String token = OAuthEncoder.decode(;
                    return new Token(token, "", response);
                } else {
                    throw new OAuthException(
                            "Response body is incorrect. Can't extract a token from this: '"
                                    + response + "'", null);

    public String getAuthorizationUrl(OAuthConfig config) {
        // Append scope if present
        if (config.hasScope()) {
            return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(),
        } else {
            return String.format(AUTHORIZE_URL, config.getApiKey(),

    public Verb getAccessTokenVerb() {
        return Verb.POST;

    public OAuthService createService(OAuthConfig config) {
        return new GoogleOAuth2Service(this, config);

    private class GoogleOAuth2Service extends OAuth20ServiceImpl {

        private static final String GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code";
        private static final String GRANT_TYPE = "grant_type";
        private DefaultApi20 api;
        private OAuthConfig config;

        public GoogleOAuth2Service(DefaultApi20 api, OAuthConfig config) {
            super(api, config);
            this.api = api;
            this.config = config;

        public Token getAccessToken(Token requestToken, Verifier verifier) {
            OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(),
            switch (api.getAccessTokenVerb()) {
            case POST:
            case GET:
                if (config.hasScope())
            Response response = request.send();
            return api.getAccessTokenExtractor().extract(response.getBody());

Create a class MyConstants, to store Client ID and Client Secret that you previously created on Google Developers Console.
package org.o7planning.tutorial.scribegoogle;

public class MyConstants {

   // Client ID
   public static final String GOOGLE_CLIENT_ID = "";

   // Client Secret
   public static final String GOOGLE_CLIENT_SECRET = "yNHBZRDB-ThCIRa29pNJEyh-";

   // Redirect URI
   public static final String GOOGLE_REDIRECT_URL = "";

package org.o7planning.tutorial.scribegoogle.test;

import java.util.Scanner;

import org.o7planning.tutorial.scribegoogle.MyConstants;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.Google2Api;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

public class Google2Example {

  private static final String NETWORK_NAME = "Google";

  private static final String PROTECTED_RESOURCE_URL = "";

  private static final String SCOPE = "";

  private static final Token EMPTY_TOKEN = null;

  public static void main(String[] args) {

      String apiKey = MyConstants.GOOGLE_CLIENT_ID;
      String apiSecret = MyConstants.GOOGLE_CLIENT_SECRET;
      String callbackUrl = MyConstants.GOOGLE_REDIRECT_URL;

      // Create OAuthService for Google OAuth 2.0
      OAuthService service = new ServiceBuilder().provider(Google2Api.class)

      Scanner in = new Scanner(;

      System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");

      Verifier verifier = null;

      Token accessToken = null;

      // Obtain the Authorization URL
      System.out.println("Fetching the Authorization URL...");
      String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
      System.out.println("Got the Authorization URL!");
      System.out.println("Now go and authorize Scribe here:");

      // Copy this URL and run in browser.

      // Copy Authorization Code in browser URL and paste to Console
      System.out.println("And paste the authorization code here");
      verifier = new Verifier(in.nextLine());

      // Trade the Request Token and Verfier for the Access Token
      System.out.println("Trading the Request Token for an Access Token...");
      accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
      System.out.println("Got the Access Token!");
      System.out.println("(if your curious it looks like this: "
              + accessToken + " )");

      // Now let's go and ask for a protected resource!
      System.out.println("Now we're going to access a protected resource...");
      OAuthRequest request = new OAuthRequest(Verb.GET,
      service.signRequest(accessToken, request);
      Response response = request.send();
      System.out.println("Got it! Lets see what we found...");

              .println("Thats it man! Go and build something awesome with Scribe! :)");
Running Google2Example:
Copy URL from the above red circled images and run them on the browser:
After you log in Google account, Google requires User to allow application to see some information of the account. Click on Accept to allow it.
Website will redirect to Callback-URL. This URL includes accessing code, and you need to copy this code:
Copy code and paste on  Java Console window:
You will receive user information: