Started on Reddits API, token fetching is done

This commit is contained in:
Jesper Wrang 2018-01-11 00:20:01 +01:00
parent df46b5bde9
commit 78374850f9
7 changed files with 71 additions and 4 deletions

View file

@ -1 +0,0 @@
s

View file

View file

@ -2,11 +2,27 @@ import React from 'react'
import {prettyScore} from 'utils'
export default class Thread extends React.Component {
constructor() {
super()
this.state = {
url: '',
thumbnailWidth: 70,
thumbnailHeight: 70,
thumbnail: '',
position: 0,
score: 0
}
}
render () {
const url = this.props.url.replace('reddit.com', 'removeddit.com')
let thumbnail
let thumbnailWidth = this.props.thread.thumbnail_width ? this.props.thread.thumbnail_width * 0.5 : 70
let thumbnailHeight = this.props.thread.thumbnail_height ? this.props.thread.thumbnail_height * 0.5 : 70
const thumbnailWidth = this.props.thread.thumbnail_width ? this.props.thread.thumbnail_width * 0.5 : 70
const thumbnailHeight = this.props.thread.thumbnail_height ? this.props.thread.thumbnail_height * 0.5 : 70
if(['self', 'default', 'image', 'nsfw'].includes(this.props.thread.thumbnail)) {
thumbnail = <a href={url} className='thumbnail thumbnail-<%= thread.thumbnail%>'></a>
@ -17,7 +33,6 @@ export default class Thread extends React.Component {
</a>
)
}
return (
<div className='thread'>

View file

@ -8,3 +8,8 @@ ReactDOM.render(
<App />,
document.getElementById("app")
)
// import Reddit from './reddit'
// Reddit.comments(1,2).then(() => console.log('lololololololol'))

View file

@ -0,0 +1,3 @@
// 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
export default 'YSidAws9twqCZg'

42
src/js/reddit/index.js Normal file
View file

@ -0,0 +1,42 @@
import clientID from './clientID'
import {json} from 'utils'
// Reddit API
const init = {
headers: {
'Authorization': ''
}
}
let hasToken = false
const tokenInit = {
headers: {
'Authorization': 'Basic ' + 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'
}
const fetchToken = () => {
if(hasToken) {
return Promise.resolve()
}
return fetch('https://www.reddit.com/api/v1/access_token', tokenInit)
.then(json)
.then(json => {
init.headers.Authorization = 'bearer ' + json.access_token;
hasToken = true
})
}
export default {
comments(subreddit, threadID) {
return fetchToken()
.then(() => console.log('hi'))
}
}

View file

@ -2,6 +2,9 @@ import SnuOwnd from 'libraries/snuownd.js'
const markdown = SnuOwnd.getParser()
// JSON parsing for fetch
export const json = x => x.json()
// Parse comments
export const parse = text => {
return markdown.render(text)