简体   繁体   中英

Accessing Vimeo API via access script using the vimeo ruby gem

I would like to grab the thumbnail urls of a list of restricted videos.

I created an app with vimeo and got an access token.

How do I use this access token to gain access to the method? When I try this:

videos = Vimeo::Advanced::Video.new("client_identifier", "client_secret", 
    :token => "access_token")
videos.get_thumbnail_urls(the_video_id)

I get

Vimeo::Advanced::RequestFailed: 401: Permission Denied, explanation: The oauth_token passed was either not valid or has expired.

My users don't have vimeo accounts so I don't see the point (or a way) have them authenticate with vimeo. I would like to grab the thumbnails of videos uploaded by the same account which created the app. My understanding is that the access token I generated ought to provide this access.

What am I missing?

Update: Here's what worked for me, based on the accepted answer

require 'httparty'

video_id = "123456789"   # substitute with the desired video ID
api_url = "https://api.vimeo.com/videos/#{video_id}/"  
auth = "Bearer access_token_generated_by_vimeo"    # use your access token
r = HTTParty.get api_url, headers: { "Authorization" => auth, "Accept" => "application/vnd.vimeo.*+json;version=3.2" }  # make sure to use the proper Accept header as recommended in the API docs
v = JSON.parse(r)
v["pictures"]["sizes"][1]["link"]

The Advanced API is Vimeo's old API. This has been deprecated, and many of the libraries still only work with this old API.

Luckily the new API is very easy and you don't really need a library. Particularly if all you want is thumbnail access.

Authentication

Take a look at the docs for Single vs. Multi user applications on the dev site . Since your users don't have vimeo accounts you should follow the instructions for a single user account (basically hard code a single access token. You can generate this token on your app page under the "Authentication" tab".

API Requests

The new API uses a much cleaner authentication and request structure. Thumbnails are provided alongside all video responses. This includes https://api.vimeo.com/videos/{video_id} , https://api.vimeo.com/me/videos , https://api.vimeo.com/channels/{channel_id}/videos and more.

Vimeo recommends you include your access token in a header (Authorization: bearer {token}) but the system does allow you to provide it through the URL (?access_token={token}).

A final request could look like

GET https://api.vimeo.com/me/videos
Authorization: bearer abcd1234

Ruby

Unfortunately I'm not familiar with ruby, so I can't help with translating this to ruby, but there's likely a great HTTP library out there that can help you.

This is what worked for me to get a video id from a query string after much experimentation (with thanks to all the above posters).

escaped_title = CGI::escape(title)
api_url = "https://api.vimeo.com/videos?query=#{escaped_title}&sort=relevant&access_token=#{@access_token}"
vimeo_response = JSON.parse(HTTParty.get api_url)
vimeo_id = vimeo_response["data"][0]["uri"]

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