简体   繁体   中英

Making sense of podbean-api

I am trying to make a site where I fetch my favorite podcasts from Podbean API .

I have worked with fetch before, but those API's were much easier to setup and there was no auth part. So that's what I am struggling with.

So this is basically what I have used before:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json))

From what I understand looking through various other threads:

  1. I need to send get request with my client_id and client_secret to the get the access_token

  2. From there on with access_token I get access and therefore can fetch date from the podcasts object.

I would appreciate any kind of guidance how to handle the auth part and obviously let me know if some of my thought processes are completely wrong.

Thanks in advance!

Ok, so I know the question is over 2 years old but I will still share my solution as I have struggled A LOT to understand how to use this API. This solution is only applicable if you are the owner of the podcast (or at least have access to the dev account).

The thing with the API is if you do not want to use auth2 (which I am still not sure how it works exactly with podbean), you have to fecth the data with a POST method not a GET and provide parameters (body and headers) and use HTTP basic authentication scheme . Their documentation is only in php but with some research you get what they are doing, the section applicable to this solution can be found here .

Here is the code:

const fetch = require("node-fetch");
const btoa = require('btoa');
const client_id = 'Enter your client id';
const client_secret = 'Enter your client secret';
const uri = 'https://api.podbean.com/v1/oauth/token';

// Base 64 encode client_id and client_secret to use basic authentication scheme
const auth = "Basic " + btoa(client_id + ':' + client_secret);

// Set POST request params
const options = {
    method: 'POST',
    body: 'grant_type=client_credentials',
    headers : {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": auth
    }
}

// Fetch
fetch(uri, options)
    .then(res => res.json())
    .then(data => console.log(data))

I hope this helps anyone who would try to use this API with javascript in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM