· 9 years ago · Oct 13, 2016, 04:24 AM
1// api key and secret
2key = "api key goes here"
3secret = "api secret goes here"
4
5var base_url = "https://api.producthunt.com/v1/"
6var token_url = base_url +"oauth/token"
7var today_post_url = base_url + "posts"
8
9
10function get_token(){
11 var token = '';
12
13 $.ajax({
14 url: token_url,
15 type: "POST",
16 async: false,
17 header: {
18 'Accept': 'application/json',
19 'Content-Type': 'application/json',
20 },
21 data: {
22 "client_id": key,
23 "client_secret": secret,
24 "grant_type": 'client_credentials'
25 },
26 success: (function(res) {
27 token = res.access_token;
28 })
29 });
30
31 return (token);
32}
33
34function get_todays_posts() {
35 token = get_token();
36 console.log(token);
37
38 $.ajax({
39 url: today_post_url,
40 header: {
41 'Accept': 'application/json',
42 'Content-Type': 'application/json',
43 'Authorization': 'Bearer ' + token
44 },
45 success: (function(res){
46 return res;
47 })
48 })
49}