John Saville
John Saville

Reputation: 51

Can the vimeo api be accessed through pure javascript?

I'm trying to build a video conglomerator using several different video hosting sites. I'm able to get videos from youtube and dailymotion with simple axios calls like:

        const response = await axios({
            method:'GET',
            url:DAILYMOTION_API_URL,
            params:{
                search:searchTerm,
                limit:limit,
            }
        });

but when I try to do the same with vimeo like:

        const response = await axios({
            method: 'GET',
            url: VIMEO_API_URL,
            params: {
                secret:CLIENT_SECRET,
                token:TOKEN,
                identifier:CLIENT_IDENTIFIER,
                query:searchTerm,
            }
        });

I get a 401 response. If I need to use python to do this I will but I'm hoping to avoid it. Is the get request formatted wrong?

Upvotes: 2

Views: 2390

Answers (1)

Larry Battle
Larry Battle

Reputation: 9178

A 401 HTTP response means your auth token or user credentials are invalid, not provided, expired or you just don't have access to the resource.

Double check that your client id and client service are valid. You can find it after creating a app on the Vimeo developer page https://developer.vimeo.com/apps/

Before you can use Vimeo's API you need to request a JWT access token.

Here's sample Nodejs 16+ code that gets a access token then pulls data from the trending videos channel.

Just install axios, npm i axios, and update CLIENT_ID and CLIENT_SECRET in the script

  • File: vimeo_trending_channel.js
// Sample code for Vimeo API v3.4 
// date created: 2022 April 15

const axios = require('axios');

const CLIENT_ID = "" REPLACE;
const CLIENT_SECRET = "" REPLACE;

const createBasicAuthValue = () => {
    return `basic ` + Buffer.from(CLIENT_ID + ":" + CLIENT_SECRET).toString("base64");
};

async function requestVimeoAccessToken(){
    const response = await axios({
        url: "https://api.vimeo.com/oauth/authorize/client",
        method: "POST",
        headers: {
            "Authorization": createBasicAuthValue(),
            "Content-Type": "application/json",
            "Accept": "application/vnd.vimeo.*+json;version=3.4"
        },
        data: {
            "grant_type": "client_credentials",
            "scope": "public"
        }
    }).catch(function (error) {
        console.log(error);
    });
    
    return {
        success : response.status === 200,
        data : response.data.access_token
    }
}

async function requestVimeoChannel(){
    const tokenResponse = await requestVimeoAccessToken();

    const response = await axios({
        url: "https://api.vimeo.com/channels/1328771",
        method: "GET",
        headers: {
            "Authorization": `Bearer ${tokenResponse.data}`,
            "Content-Type": "application/json",
            "Accept": "application/vnd.vimeo.*+json;version=3.4"
        },
    }).catch(function (error) {
        console.log(error);
    });
    
    return {
        success : response.status === 200,
        data : response.data
    }
}

async function init(){
    const channelResponse = await requestVimeoChannel() 
    
    console.log( channelResponse.data.name === "Trending Videos" );
}
init();

More info:

Upvotes: 2

Related Questions