mirror of
https://github.com/gurnec/removeddit.git
synced 2026-03-11 08:54:27 +00:00
Stop using OAuth2 with the Reddit API
None of the API endpoints being used require authentication, its use was slowing things down a bit for no advantage.
This commit is contained in:
parent
69e802e94f
commit
3230ebc0d8
2 changed files with 60 additions and 65 deletions
|
|
@ -1,47 +1,47 @@
|
|||
import { fetchJson } from '../../utils'
|
||||
|
||||
// Change this to your own client ID: https://www.reddit.com/prefs/apps
|
||||
// The app NEEDS TO BE an installed app and NOT a web apps
|
||||
|
||||
// Current using dummy ID from throwaway
|
||||
const clientID = 'ZmYXJ5RaSDhF-77YaFulWw'
|
||||
|
||||
// Token for reddit API
|
||||
let token
|
||||
|
||||
const getToken = () => {
|
||||
// We have already gotten a token
|
||||
if (token !== undefined) {
|
||||
return Promise.resolve(token)
|
||||
}
|
||||
|
||||
// Headers for getting reddit api token
|
||||
const tokenInit = {
|
||||
headers: {
|
||||
Authorization: `Basic ${window.btoa(`${clientID}:`)}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
},
|
||||
method: 'POST',
|
||||
body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE`
|
||||
}
|
||||
|
||||
return fetchJson('https://www.reddit.com/api/v1/access_token', tokenInit)
|
||||
.then(response => {
|
||||
token = response.access_token
|
||||
return token
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('reddit.getToken ->')
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
// Get header for general api calls
|
||||
export const getAuth = () => {
|
||||
return getToken()
|
||||
.then(token => ({
|
||||
headers: {
|
||||
Authorization: `bearer ${token}`
|
||||
}
|
||||
}))
|
||||
}
|
||||
//import { fetchJson } from '../../utils'
|
||||
//
|
||||
//// Change this to your own client ID: https://www.reddit.com/prefs/apps
|
||||
//// The app NEEDS TO BE an installed app and NOT a web apps
|
||||
//
|
||||
//// Current using dummy ID from throwaway
|
||||
//const clientID = 'ZmYXJ5RaSDhF-77YaFulWw'
|
||||
//
|
||||
//// Token for reddit API
|
||||
//let token
|
||||
//
|
||||
//const getToken = () => {
|
||||
// // We have already gotten a token
|
||||
// if (token !== undefined) {
|
||||
// return Promise.resolve(token)
|
||||
// }
|
||||
//
|
||||
// // Headers for getting reddit api token
|
||||
// const tokenInit = {
|
||||
// headers: {
|
||||
// Authorization: `Basic ${window.btoa(`${clientID}:`)}`,
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
// },
|
||||
// method: 'POST',
|
||||
// body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE`
|
||||
// }
|
||||
//
|
||||
// return fetchJson('https://www.reddit.com/api/v1/access_token', tokenInit)
|
||||
// .then(response => {
|
||||
// token = response.access_token
|
||||
// return token
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.error('reddit.getToken ->')
|
||||
// throw error
|
||||
// })
|
||||
//}
|
||||
//
|
||||
//// Get header for general api calls
|
||||
//export const getAuth = () => {
|
||||
// return getToken()
|
||||
// .then(token => ({
|
||||
// headers: {
|
||||
// Authorization: `bearer ${token}`
|
||||
// }
|
||||
// }))
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { fetchJson, chunk } from '../../utils'
|
||||
import { getAuth } from './auth'
|
||||
|
||||
const baseURL = 'https://api.reddit.com'
|
||||
|
||||
const errorHandler = (error, from) => {
|
||||
console.error(from + ': ' + error)
|
||||
|
|
@ -8,32 +9,26 @@ const errorHandler = (error, from) => {
|
|||
|
||||
// Return the post itself
|
||||
export const getPost = (subreddit, threadID) => (
|
||||
getAuth()
|
||||
.then(auth => fetchJson(`https://oauth.reddit.com/comments/${threadID}.json?limit=1`, auth))
|
||||
fetchJson(`${baseURL}/comments/${threadID}.json?limit=1`)
|
||||
.then(thread => thread[0].data.children[0].data)
|
||||
.catch(error => errorHandler(error, 'reddit.getPost'))
|
||||
)
|
||||
|
||||
//// Fetch multiple threads (via the info endpoint)
|
||||
//export const getThreads = threadIDs => {
|
||||
// return getAuth()
|
||||
// .then(auth => fetchJson(`https://oauth.reddit.com/api/info?id=${threadIDs.map(id => `t3_${id}`).join()}`, auth))
|
||||
//export const getThreads = threadIDs => (
|
||||
// fetchJson(`${baseURL}/api/info?id=${threadIDs.map(id => `t3_${id}`).join()}`)
|
||||
// .then(response => response.data.children.map(threadData => threadData.data))
|
||||
// .catch(error => errorHandler(error, 'reddit.getThreads'))
|
||||
//}
|
||||
//)
|
||||
|
||||
// Helper function that fetches a list of comments
|
||||
const fetchComments = (commentIDs, auth) => {
|
||||
return fetchJson(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth)
|
||||
const fetchComments = (commentIDs) => (
|
||||
fetchJson(`${baseURL}/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`)
|
||||
.then(results => results.data.children.map(({data}) => data))
|
||||
}
|
||||
)
|
||||
|
||||
export const getComments = commentIDs => {
|
||||
return getAuth()
|
||||
.then(auth => (
|
||||
Promise.all(chunk(commentIDs, 100)
|
||||
.map(ids => fetchComments(ids, auth)))
|
||||
.then(results => results.flat())
|
||||
))
|
||||
export const getComments = commentIDs => (
|
||||
Promise.all(chunk(commentIDs, 100).map(ids => fetchComments(ids)))
|
||||
.then(results => results.flat())
|
||||
.catch(error => errorHandler(error, 'reddit.getComments'))
|
||||
}
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue