Hey there, fellow Go enthusiast! Ready to dive into the world of Vimeo API integration? You're in for a treat. We'll be using the awesome go-vimeo package to make our lives easier. Buckle up, and let's get coding!
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go project:
mkdir vimeo-integration cd vimeo-integration go mod init vimeo-integration
Now, let's grab that go-vimeo package:
go get github.com/vimeo/go-vimeo
Time to get cozy with the Vimeo API. First, let's initialize our Vimeo client:
package main import ( "github.com/vimeo/go-vimeo/vimeo" "golang.org/x/oauth2" ) func main() { config := oauth2.Config{} token := &oauth2.Token{AccessToken: "YOUR_ACCESS_TOKEN"} client := config.Client(oauth2.NoContext, token) v := vimeo.NewClient(client, nil) }
Replace "YOUR_ACCESS_TOKEN" with your actual token, and you're good to go!
Let's grab some user info:
user, _, err := v.Users.Get("user123") if err != nil { // Handle error } fmt.Printf("User: %s\n", user.Name)
Want video details? Coming right up:
video, _, err := v.Videos.Get("video456") if err != nil { // Handle error } fmt.Printf("Video: %s\n", video.Name)
Time to share your masterpiece with the world:
file, _ := os.Open("awesome_video.mp4") defer file.Close() video, _, err := v.Videos.UploadVideo(&vimeo.VideoUploadOptions{ Name: "My Awesome Video", Description: "This video will blow your mind!", Privacy: vimeo.PrivacyAnybody, }, file) if err != nil { // Handle error } fmt.Printf("Uploaded video ID: %s\n", video.URI)
Let's find some cool videos:
searchOpts := &vimeo.SearchOptions{ Query: "awesome", Sort: "relevant", } videos, _, err := v.Videos.Search(searchOpts) if err != nil { // Handle error } for _, video := range videos { fmt.Printf("Found video: %s\n", video.Name) }
Keep it secret, keep it safe:
updateOpts := &vimeo.VideoUpdateOptions{ Privacy: vimeo.PrivacyPassword, Password: "supersecret", } _, _, err := v.Videos.Update("video789", updateOpts) if err != nil { // Handle error }
Thumbnails can make or break a video. Let's grab 'em:
thumbnails, _, err := v.Videos.GetThumbnails("video101") if err != nil { // Handle error } for _, thumb := range thumbnails { fmt.Printf("Thumbnail URL: %s\n", thumb.Link) }
Always check for errors, folks! It's not just good practice, it'll save you headaches down the road. Also, keep an eye on those rate limits. Vimeo's pretty generous, but don't push your luck!
Testing is your friend. Here's a quick example:
func TestGetUser(t *testing.T) { v := setupTestClient() user, _, err := v.Users.Get("testuser") if err != nil { t.Errorf("Error getting user: %v", err) } if user.Name != "Test User" { t.Errorf("Expected user name 'Test User', got '%s'", user.Name) } }
And there you have it! You're now armed and dangerous with Vimeo API integration skills. Remember, the official Vimeo API docs are your best friend for diving deeper.
Keep coding, keep creating, and most importantly, have fun with it! If you want to see a complete example, check out my GitHub repo [link to your repo]. Happy coding!