-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelpers.go
58 lines (50 loc) · 1.64 KB
/
helpers.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
package hangups
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
func GetAuthHeaders(sapisid string) map[string]string {
originUrl := "https://talkgadget.google.com"
timestampMsec := time.Now().Unix() * 1000
authString := fmt.Sprintf("%d %s %s", timestampMsec, sapisid, originUrl)
hash := sha1.New()
hash.Write([]byte(authString))
hashBytes := hash.Sum(nil)
hexSha1 := hex.EncodeToString(hashBytes)
sapisidHash := fmt.Sprintf("SAPISIDHASH %d_%s", timestampMsec, hexSha1)
return map[string]string{"authorization": sapisidHash, "x-origin": originUrl, "x-goog-authuser": "0"}
}
func ApiRequest(endpointUrl, contentType, responseType, cookies, sapisid string, headers map[string]string, payload []byte) ([]byte, error) {
authHeaders := GetAuthHeaders(sapisid)
for headerKey, headerVal := range authHeaders {
headers[headerKey] = headerVal
}
headers["cookie"] = cookies
headers["content-type"] = contentType
// This header is required for Protocol Buffer responses, which causes
// them to be base64 encoded:
headers["X-Goog-Encode-Response-If-Executable"] = "base64"
urlObject, _ := url.Parse(endpointUrl)
urlParams := url.Values{}
urlParams.Set("alt", responseType)
urlObject.RawQuery = urlParams.Encode()
req, err := http.NewRequest("post", urlObject.String(), bytes.NewBufferString(string(payload)))
for headerKey, headerVal := range headers {
req.Header.Set(headerKey, headerVal)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return nil, err
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return bodyBytes, nil
}