/examples/username-password-auth.php

https://github.com/cbcolvard/podio-php · PHP · 54 lines · 26 code · 5 blank · 23 comment · 3 complexity · fdd70e1f5280031a3b5ef59ac3094c1c MD5 · raw file

  1. <?php
  2. /*
  3. This example shows you how to authenticate using the username and password flow.
  4. You are stringly encouraged to read about the different authentication methods
  5. at https://developers.podio.com/authentication to determine which is best for
  6. your use.
  7. To run this example you must perform some quick configuration. Follow these steps:
  8. * Go to https://podio.com/settings/api and create an API client id and client secret. The domain you use must be the domain you will be running these examples under (the domain "localhost" will always work).
  9. * Create a copy of the file config.sample.php and call it config.php
  10. * Open this new config.php and fill in your client id, client secret and your Podio username and password
  11. * Run this file in your browser.
  12. */
  13. ?>
  14. <html>
  15. <head>
  16. <title>Username and Password authentication example</title>
  17. </head>
  18. <body>
  19. <?php
  20. // Include the config file and the Podio library
  21. require_once 'config.php';
  22. require_once '../PodioAPI.php';
  23. // Setup the API client reference. Client ID and Client Secrets are defined
  24. // as constants in config.php
  25. Podio::setup(CLIENT_ID, CLIENT_SECRET);
  26. // Use Podio::is_authenticated() to check is there's already an active session.
  27. // If there is you can make API calls right away.
  28. if (!Podio::is_authenticated()) {
  29. // Authenticate using your username and password. Both are defined as constants
  30. // in config.php
  31. Podio::authenticate('password', array('username' => USERNAME, 'password' => PASSWORD));
  32. print "You have been authenticated. Wee!<br>";
  33. $access_token = Podio::$oauth->access_token;
  34. print "Your access token is {$access_token}<br><br>";
  35. print "The access token is automatically saved in a session for your convenience.<br><br>";
  36. }
  37. else {
  38. print "You were already authenticated and no authentication happened. Close and reopen your browser to start over.<br><br>";
  39. }
  40. // Now you can start making API calls. E.g. get your user status
  41. $status = PodioUserStatus::get();
  42. print "Your user id is <b>{$status->user->id}</b> and you have <b>{$status->inbox_new}</b> unread messages in your inbox.<br><br>";
  43. ?>
  44. </body>
  45. </html>