podio API JS browser authentication and API calls

524 views Asked by At

I'm quite newbee on API thing, been reading the documentation here for JAVASCRIPT client but I can't make things work, even on authentication part. I already have the client ID and ClientSecret from PODIO itself.

Basically, I want to get all podio data in a workspace in a JSON format using client side (browser only).

I've downloaded the library here and created an HTML file on my localhost and link the podio-js with following code. Getting this error "podio-js.js:1 Uncaught ReferenceError: require is not defined at podio-js.js:1". Do I need to install something such that loader thing to make this work?

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<script type="text/javascript" src="lib/podio-js.js"></script>  
<script type="text/javascript">
var podio = new PodioJS({
  authType: 'server', 
  clientId: 'foo', 
  clientSecret:  'foo'
});
var redirectURL = 'http://localhost/PODIO-JS/podio-js-master/PODIO_CLIENT.html';

// Your request handler (for example in ExpressJS)
var action = function(request, response) {
var authCode = request.query.code;
var errorCode = request.query.error;

podio.isAuthenticated().then(function() {
  // Ready to make API calls...

}).catch(function(err) {

  if (typeof authCode !== 'undefined') {
    podio.getAccessToken(authCode, redirectURL, function(err, response) {
      // make API calls here 
        console.log (responsedata);
    }); 
  } else if (typeof errorCode !== 'undefined') {
    // a problem occured
    console.log(request.query.error_description);
  } else {
    // start authentication via link or redirect
    console.log(podio.getAuthorizationURL(redirectURL));
  }
});

</script>


    </head>
<body>

</body>
</html>
1

There are 1 answers

0
domokun On BEST ANSWER

You can only use the syntax PodioJS = require('podio-js') if you're working in an AMD environment, typically using requirejs.

You're using a good ol' HTML page instead, which means you have to follow the second part of the browser usage section found here: https://github.com/podio/podio-js#browser


From within the podio-js folder:

npm install -g browserify
npm run bundle

and then include dist/podio-js.js using a tag in your HTML page.

Note: once you've bundled the source, you can copy paste the compiled file wherever you want.