Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 2.9 KB

stackoverflow.md

File metadata and controls

76 lines (56 loc) · 2.9 KB

StackOverflow

EveryAuth is the easiest way for your app to access StackOverflow APIs. Make sure to follow the EveryAuth setup instructions before adding StackOverflow support to your app.

Authorize access to StackOverflow

Add the following route to your Express app using EveryAuth middleware:

import everyauth from "@fusebit/everyauth-express";

// Add support for users to authorize access to StackOverflow APIs
router.use(
  "/stackoverflow",
  everyauth.authorize("stackoverflow", {
    finishedUrl: "/stackoverflow/finished",
    mapToUserId: async (req) => "user-123", // req.user.id in production
  })
);

router.get("/stackoverflow/finished", (req, res) => {
  res.send("Thank you for authorizing access to StackOverflow!");
});

When you want users of your app to authorize access to StackOverflow so that your app can call StackOverflow APIs on their behalf, redirect their browser to the /stackoverflow endpoint above.

Call StackOverflow APIs

After a user has authorized your app to call StackOverflow APIs, you can use any generic HTTP client for Node.js SDK to make the call.

But first, use EveryAuth to resolve the identifier of the user of your app to StackOverflow credentials:

import everyauth from "@fusebit/everyauth-express";

// Get StackOverflow credentials for a specific user of your app
const userId = "user-123"; // req.user.id in production
const userCredentials = await everyauth.getIdentity("stackoverflow", userId);

The StackOverflow credential returned has the following schema:

{
  "accessToken": "(0wR...", // Current access token to StackOverflow APIs
  "native": {
    "timestamp": 1649812976996, // Time the credential was established
    "client_key": "V)1...", // The Stack Overflow client key
    "access_token": "(0wR..." // Current access token to StackOverflow APIs
  },
}

Configure StackOverflow service

The shared StackOverflow OAuth client that EveryAuth provides out of the box supports basic permissions that allow you to call StackOverflow APIs right away. The following OAuth scopes are included:

  • read_inbox
  • private_info
  • no_expiry

If you need to address more advanced scenarios, you need to create your own OAuth client and configure EveryAuth to use it. First create an OAuth client in StackOverflow, and then use the EveyAuth CLI to configure the StackOverflow service to use it:

everyauth service set stackoverflow \
  --scope "{your-scopes}" \
  --clientId "{your-client-id}" \
  --clientSecret "{your-client-secret}

Resources

Introduction to EveryAuth
The StackOverflow APIs
Create StackOverflow OAuth client
Fusebit