-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
110 lines (94 loc) · 2.63 KB
/
message.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"net/http"
"encoding/json"
"io/ioutil"
"bytes"
"log"
)
type Message struct {
MessageURL, Token, Platform, QueueType string
}
type LinkedInCall struct {
Comment string
Content map[string]string
Visibility map[string]string
}
type FacebookCall struct {
Message string `json:"message"`
Access_token string `json:"access_token"`
}
type LinkedInResponse struct {
UpdateKey, UpdateURL string
}
type FacebookResponse struct {
Id string
}
func (m *Message) LinkedIn() ([]byte, LinkedInResponse) {
cont := map[string]string{
"title": "Hack Reactor and You",
"description": "pillows",
"submitted-url": m.MessageURL,
"submitted-image-url": "https://golang.org/doc/gopher/frontpage.png",
}
vis := map[string]string{
"code": "anyone",
}
bod := LinkedInCall{
"Pillow Talk",
cont,
vis,
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(bod)
// equivalent to Marshal, except intended for streams
// NewEncoder accepts an io.Writer, so it's generally intended for streams
// Encode writes the JSON encoding of the argument passed to that stream
url := `https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=`+m.Token+`&format=json`
req, err := http.NewRequest("POST", url, b)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-li-format", "json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
defer resp.Body.Close()
// defer moves this to the end of the function
var lrs LinkedInResponse
fmt.Println("response Status: ", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &lrs)
// decode body out of JSON into &lrs
fmt.Println("response Body:", string(body))
fmt.Println(lrs)
return body, lrs;
}
func (m *Message) Facebook() ([]byte, FacebookResponse) {
fbjson := FacebookCall{m.MessageURL, m.Token}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(fbjson)
fmt.Println(b.String());
url := `https://graph.facebook.com/me/feed`
req, err := http.NewRequest("POST", url, b)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
var frs FacebookResponse
fmt.Println("response Status: ", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &frs)
fmt.Println("response Body:", string(body))
fmt.Println(frs)
return body, frs;
}