You are viewing limited content. For full access, please sign in.

Question

Question

Authorizing app.laserfiche.com with the Google API

asked on April 29, 2022 Show version history

Anyone else ever tried to use the Google API with app.laserfiche.com (Authorized Javascript Origin) for the cloud version and have Google reject it on requests? It seems it just doesn't work with this specific URL, I can use localhost and other custom URLs in my on-prem servers but the same exact code does not work in cloud even after adding https://app.laserfiche.com

Here is the code I have been using for years with many domains, as long as I add it to the list of URI's

<script type="text/javascript">


      // Your Client ID can be retrieved from your project in the Google
      // Developer Console, https://console.developers.google.com
      var CLIENT_ID = 'XXXXX';

      var SCOPES = ["https://www.googleapis.com/auth/calendar"];

      /**
       * Check if current user has authorized this application.
       */
      function checkAuth() {


        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
          }, handleAuthResult);
      }

      /**
       * Handle response from authorization server.
       *
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
          authorizeDiv.style.display = 'none';
          loadCalendarApi();

        } else {

          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
      }

      /**
       * Initiate auth flow in response to user clicking authorize button.
       *
       * @param {Event} event Button click event.
       */
      function handleAuthClick(event) {
        gapi.auth.authorize(
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
          handleAuthResult);
        return false;
      }

      /**
       * Load Google Calendar client library. List upcoming events
       * once client library is loaded.
       */
      function loadCalendarApi() {
        gapi.client.load('calendar', 'v3', addEvent);
      }

      /**
       * Print the summary and start datetime/date of the next ten events in
       * the authorized user's calendar. If no events are found an
       * appropriate message is printed.
       */
	   
function addEvent() {
	   
	  var startTime = $('select[id^=Field6]').val();
	  var endTime = $('select[id^=Field7]').val();
	  var calendar = $('#q9 input').val();

var arrcal = calendar.split("/");
if (arrcal[0] < 2)
{arrcal[0] = "0"+arrcal[0];}
if (arrcal[1] < 2)
{arrcal[1] = "0"+arrcal[1];}

var formDate = arrcal[2]+"-"+arrcal[0]+"-"+arrcal[1];
var start = formDate+startTime;
var end = formDate+endTime;


var event = {
  'summary': 'LF Form Test',
  'location': 'Phone',
  'description': 'Test Event',
  'start': {
    'dateTime': start,
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'dateTime':  end,
    'timeZone': 'America/Los_Angeles'
  },
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10}
    ]
  }
};

var request = gapi.client.calendar.events.insert({
  'calendarId': 'primary',
  'resource': event
});

request.execute(function(event) {
var strevent = event.htmlLink;
if (strevent.indexOf("google.com") >= 0)
{
  appendPre('Event Created');
} else 
{appendPre('Failed to create event');}
});
	   
	   }
	   
      function listUpcomingEvents() {
        var request = gapi.client.calendar.events.list({
          'calendarId': 'primary',
          'timeMin': (new Date()).toISOString(),
          'showDeleted': false,
          'singleEvents': true,
          'maxResults': 10,
          'orderBy': 'startTime'
        });

        request.execute(function(resp) {
          var events = resp.items;
          appendPre('Upcoming events:');

          if (events.length > 0) {
            for (i = 0; i < events.length; i++) {
              var event = events[i];
              var when = event.start.dateTime;
              if (!when) {
                when = event.start.date;
              }
              appendPre(event.summary + ' (' + when + ')')
            }
          } else {
            appendPre('No upcoming events found.');
          }

        });
      }

      /**
       * Append a pre element to the body containing the given message
       * as its text node.
       *
       * @param {string} message Text to be placed in pre element.
       */
      function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }



    </script>
    <script src="https://apis.google.com/js/client.js" gapi_processed="true">
    </script>
  
  
  <button id="start" onclick="checkAuth()" type="button">
  Create Event
  </button>
    <div id="authorize-div" style="display: none">
      <span>Authorize access to Google Calendar API</span>
      <!--Button for the user to click to initiate auth sequence -->
      <button id="authorize-button" onclick="handleAuthClick(event)" type="button">
        Authorize
      </button>
    </div>
    <pre id="output"></pre>

 

 

0 0

Replies

replied on May 2, 2022

Where is that code hosted (including URL path)? What are you trying to do? I would expect the OAuth Redirect URI to specify a path.

0 0
replied on May 2, 2022 Show version history

This is hosted in Laserfiche Forms Cloud, in a custom HTML object.

 

I have always used a redirect URI of just the base domain in the past

 

Edit: I meant javascript origina URI, what is a redirect URI?

0 0
replied on May 2, 2022

According to OAuth 2.0 RFC authorization server SHOULD require the client to  provide the complete redirection URI.

In this case you may want to try to register/use the full Laserfiche Form URL (without query strings) as the redirect URI.

0 0
replied on May 3, 2022

I think this documentation is for a different API. I just checked and our working implementations have no redirect URI enabled, since we do not redirect any traffic after using the API. We simply create a calendar entry.

The problem is with the javascript origin for sure, I can change origins and it works, but it will not acccept app.laserfiche.com as an origin.

0 0
replied on May 3, 2022 Show version history

Oh wow! It does work, it just takes Google awhile after adding a URL before it decides to accept it. I simply tried again this morning and it authroized just fine. Originally I had just added the domain before immediatly testing because I wanted to verify it would work in LF cloud, didn't know you had to wait overnight or whatever.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.