DEV Community

Khor
Khor

Posted on

1 3

Live Code Snippets To Instantly Test Any API Endpoints of Any OAuth/OAuth2 Providers

We have all been there where we pore over poorly documented OAuth API endpoints, some of which are already deprecated or even discontinued, before finding one that we believe can return some data that we want about a user, or can perform something on behalf of the user. Before we can test that endpoint, we spent more countless hours hacking together a social login button on our front-end code, which will communicate with the back-end, to complete the OAuth flow to acquire access token from the OAuth provider, e.g., Facebook, Twitter, Github, etc., and then finally, the moment of truth - calling the API endpoint with the access token.

And THEN - the endpoint does not do what we expected because its behavior has changed or the documentation is inaccurate.

No developer should EVER have to go through this! Thus we cobbled together a site with all the ready-made OAuth snippets to create a social login bootstrap button for every single popular provider, but that's not all, the site includes a snippet for calling every popular API endpoint of those providers.

Sounds unbelievable? Let's spin the wheel to pick a random action to perform on an OAuth provider.

Here we go.......... . . . . . .'Search for repository in Github'

You can head over here to test the Javascript snippet to 'search for repository in Github' instantly, and also tweak it to call other Github API endpoints.

If you want to jump the gun, and test other OAuth providers, head over here.

Here is a brief description of the working code.

Firstly, the code is short, i.e., < 15 lines, all front-end Javascript, with no backend. Secondly, it has few external requirements.

$('#github-button').on('click', function() {
  // Initialize with your OAuth.io app public key
  OAuth.initialize('txp2rKYpuKZXaaYC5kB-m13KnVE');

  // Use popup for oauth
  // Alternative is redirect
  OAuth.popup('github').then(provider => {

    // Got the OAuth provider object, which contains access token, etc.
    console.log('provider:', provider);

    provider.get('/search/repositories?q=oauth-io').then(data =>            {
      console.log('Repositories:', data);
    });
  });
})

Necessary requirements:

If you want to use the Javascript snippet on your own webpage, you can just copy-and-paste it but note that you need https://oauth.io to make it work. If you are interested in the entire setup, I can do a separate post.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️