Comments

Tri Tran created an issue. See original summary.

tri tran’s picture

1/ Create a GG App
2/ Create new html page, insert code below:

<head>
  <script src="https://apis.google.com/js/client:platform.js?onload=renderButton" async defer></script>
  <meta name="google-signin-client_id" content="{INPUT_CLIENT_ID_HERE}">
</head>

<!-- Display Google sign-in button -->
<div id="gSignIn"></div>

<script type="text/javascript">
  // Render Google Sign-in button
  function renderButton() {
    gapi.signin2.render('gSignIn', {
      'scope': 'profile email',
      'width': 240,
      'height': 50,
      'longtitle': true,
      'theme': 'dark',
      'onsuccess': onSuccess,
      'onfailure': onFailure
    });
  }

  // Sign-in success callback
  function onSuccess(googleUser) {
    // Get the Google profile data (basic)
    console.log(googleUser);
  }

  // Sign-in failure callback
  function onFailure(error) {
    alert(error);
  }
</script>

3/ Run page on browser, after that click Sign In button.
4/ Process with Google UI. To the end, Google will response the credential:
- access_token
- id
- expires_at

5/ Get the credential above and input to param of API, like this:

var settings = {
  "url": "https://YOUR_DOMAIN/oauth/token",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "SSESS9232faa2d2e66e349016e73207c2d551=T5oMHbkEvN3QJQzN1ms9nBCPl_vOY2QIv4V4Ofhh_LQ"
  },
  "data": {
    "grant_type": "google_login_grant",
    "client_id": "<client_id>",
    "client_secret": "<client_secret>",
    "google_access_token": "<access_token>",
    "google_user_id": "<id>",
    "google_token_expires_in": "<expires_at>"
  }
};

$.ajax(settings).done(function (response) {
  console.log(response);
});
tri tran’s picture

Issue summary: View changes
phthlaap’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

goodboy’s picture

4. Process with Google UI. To the end, Google will response the credential:
- access_token
- id
- expires_at

Google didn't respond id parameter, I found id_token Google parameter and it used on validateUser() function.

5. Is it required "Cookie" parameter on header?
Can we don't use "client_secret" parameter (it's not required parameter on Simple Oath module)?

Are these settings correct? In which modules can I see the use of these parameters?

"google_access_token": "<access_token>",
"google_user_id": "<id>",
"google_token_expires_in": "<expires_at>"
khoavd’s picture

Google has been updated APIS , please use this new quickstart code in this guide : https://developers.google.com/identity/oauth2/web/guides/migration-to-gi...

khoavd’s picture

<!DOCTYPE html>
<html>
  <head>
    <script src="https://accounts.google.com/gsi/client" onload="initClient()" async defer></script>
  </head>
  <body>
    <script>
      var client;
      var access_token;

      function initClient() {
        client = google.accounts.oauth2.initTokenClient({
          client_id: 'YOUR_CLIENT_ID',
          scope: 'https://www.googleapis.com/auth/calendar.readonly \
                  https://www.googleapis.com/auth/contacts.readonly',
          callback: (tokenResponse) => {
            access_token = tokenResponse.access_token;
            console.log(tokenResponse);
          },
        });
      }
      function getToken() {
        client.requestAccessToken();
      }
      function revokeToken() {
        google.accounts.oauth2.revoke(access_token, () => {console.log('access token revoked')});
      }
      function loadCalendar() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://www.googleapis.com/calendar/v3/calendars/primary/events');
        xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
        xhr.send();
      }
    </script>
    <h1>Google Identity Services Authorization Token model</h1>
    <button onclick="getToken();">Get access token</button><br><br>
    <button onclick="loadCalendar();">Load Calendar</button><br><br>
    <button onclick="revokeToken();">Revoke token</button>
  </body>
</html>