The Old Reader

News

TheOldReader API: A Comprehensive Guide with JavaScript Examples

TheOldReader is a popular RSS reader that offers a public API for third-party developers to access its data. This API allows developers to create apps that integrate with the reader and retrieve information such as articles, categories, subscriptions, and more.

In this guide, we will explore TheOldReader API and provide examples of how to use it with JavaScript.

Authentication

To access TheOldReader API, you will need to authenticate your app with OAuth 2.0. To do so, you will need to create an account with TheOldReader and register your app.

Once you've registered your app, you can use the following JavaScript code to authenticate your app and retrieve an access token:

const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const redirectUri = 'https://example.com/oauth_callback';
const authorizationUrl = `https://theoldreader.com/oauth/authorize?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}`;
const tokenUrl = 'https://theoldreader.com/oauth/token';

// Redirect the user to the authorization URL
window.location = authorizationUrl;

// Once the user has authorized your app, retrieve the authorization code from the URL:
const authorizationCode = new URL(window.location).searchParams.get('code');

// Exchange the authorization code for an access token
const response = await fetch(tokenUrl, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: `client_id=${clientId}&client_secret=${clientSecret}&grant_type=authorization_code&code=${authorizationCode}&redirect_uri=${redirectUri}`,
});
const { access_token } = await response.json();

// Use the access token in subsequent API requests

Retrieving the User's Feeds

Once you have authenticated your app, you can retrieve the user's feeds by making a GET request to the /reader/feeds endpoint. The response will include information about the user's feeds, such as the feed ID, title, and URL.

const response = await fetch('https://theoldreader.com/reader/feeds', {
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
});
const { subscriptions } = await response.json();

// Iterate over the user's subscriptions and log the title and URL of each feed
subscriptions.forEach(({ title, url }) => {
  console.log(`${title}: ${url}`);
});

Retrieving Articles from a Feed

To retrieve articles from a specific feed, you can make a GET request to the /reader/atom/feed/{feedId} endpoint, where {feedId} is the ID of the feed you want to retrieve.

const feedId = 'FEED_ID';
const response = await fetch(`https://theoldreader.com/reader/atom/feed/${feedId}`, {
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
});
const text = await response.text();

// Parse the response XML using a library such as xmldom
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'application/xml');
const entries = doc.getElementsByTagName('entry');

// Iterate over the entries and log the title and URL of each article
for (let i = 0; i < entries.length; i++) {
  const link = entries[i].querySelector('link');
  console.log(`${entries[i].querySelector('title').textContent}: ${link.getAttribute('href')}`);
}

Adding a Subscription

To add a new subscription to the user's account, you can make a POST request to the /reader/api/0/subscription/quickadd endpoint.

const url = 'FEED_URL';
const response = await fetch('https://theoldreader.com/reader/api/0/subscription/quickadd', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
  body: `quickadd=${encodeURIComponent(url)}`,
});

Conclusion

TheOldReader API provides a comprehensive set of endpoints for developers to access RSS reader data. With the examples provided in this guide, you should have a good starting point to begin building your own app and integrating with TheOldReader. Happy coding!

Related APIs in News