Last active
October 25, 2016 17:42
-
-
Save Katamori/ab588bcf905d902bb9f44624dc823aaf to your computer and use it in GitHub Desktop.
Reddit API: Application only OAuth Basic Example using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//made with jQuery 3.1.1. | |
//attach this function to any HTML action and you'll get the response | |
//- Katamori, 10-25-2016 | |
function Init(){ | |
clientID = //ID of the Reddit app you registered, as a string//; | |
clientSec = //secret part of the same app; | |
//Authorization to Reddit API | |
$.ajax({ | |
type: 'POST', | |
url: "https://www.reddit.com/api/v1/access_token", | |
beforeSend: function(xhr) { | |
xhr.setRequestHeader("Authorization", "Basic " + btoa(clientID + ":" + clientSec)); | |
}, | |
data: | |
{ | |
grant_type: "client_credentials", | |
user: clientID, | |
password: clientSec | |
}, | |
success: function(response) { | |
//main connection itself - yes, another AJAX request if the previous one is successfull | |
$.ajax({ | |
type: 'GET', | |
url: "https://oauth.reddit.com/r/mealtimevideos/about", //full list of possible "cases": https://www.reddit.com/dev/api | |
//this is the "critical" part: use the "access_token" you've got to get access | |
beforeSend: function(xhr) { | |
xhr.setRequestHeader("Authorization", response.token_type + " " + response.access_token); | |
}, | |
success: function(data) { console.log(data); } //check your Dev Console ;) | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment