forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_bulletin.go
More file actions
82 lines (68 loc) · 1.98 KB
/
post_bulletin.go
File metadata and controls
82 lines (68 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"flag"
"fmt"
"log"
"code.google.com/p/google-api-go-client/youtube/v3"
)
var (
message = flag.String("message", "", "Text message to post")
videoID = flag.String("videoid", "", "ID of video to post")
playlistID = flag.String("playlistid", "", "ID of playlist to post")
)
func main() {
flag.Parse()
// A bulletin must contain a message and may also contain a video or a
// playlist. You can post a message with or without an accompanying video
// or playlist, but you can't post a video and playlist at the same time.
if *message == "" {
log.Fatalf("Please provide a message.")
}
if *videoID != "" && *playlistID != "" {
log.Fatalf("You cannot post a video and a playlist at the same time.")
}
client, err := buildOAuthHTTPClient(youtube.YoutubeScope)
if err != nil {
log.Fatalf("Error building OAuth client: %v", err)
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
// Start making YouTube API calls.
parts := "snippet"
bulletin := &youtube.Activity{
Snippet: &youtube.ActivitySnippet{
Description: *message,
},
}
if *videoID != "" || *playlistID != "" {
parts = "snippet,contentDetails"
// The resource ID element value differs depending on
// whether a playlist or a video is being posted.
var resourceId *youtube.ResourceId
switch {
case *videoID != "":
resourceId = &youtube.ResourceId{
Kind: "youtube#video",
VideoId: *videoID,
}
case *playlistID != "":
resourceId = &youtube.ResourceId{
Kind: "youtube#playlist",
PlaylistId: *playlistID,
}
}
bulletin.ContentDetails = &youtube.ActivityContentDetails{
Bulletin: &youtube.ActivityContentDetailsBulletin{
ResourceId: resourceId,
},
}
}
call := service.Activities.Insert(parts, bulletin)
_, err = call.Do()
if err != nil {
log.Fatalf("Error making API call to post bulletin: %v", err.Error())
}
fmt.Println("The bulletin was posted to your channel.")
}