/
googledrive.go
146 lines (122 loc) · 3.23 KB
/
googledrive.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
_ "encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"regexp"
_ "strings"
)
func connectPOST(apiURL string, resource string, data url.Values, headerMap map[string]string) (string, http.Header, error) {
u, _ := url.ParseRequestURI(apiURL)
u.Path = resource
urlStr := fmt.Sprintf("%v", u)
var encodedData *bytes.Buffer
client := &http.Client{}
if data != nil {
encodedData = bytes.NewBufferString(data.Encode())
} else {
encodedData = nil
}
req, _ := http.NewRequest("POST", urlStr, encodedData)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if headerMap != nil {
for k, v := range headerMap {
req.Header.Add(k, v)
}
}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
bodystring := string(body)
if resp.StatusCode != 200 {
return bodystring, resp.Header, fmt.Errorf("%s", resp.Status)
}
return bodystring, resp.Header, nil
}
func newfileUploadRequest(uri string, paramName, mypath string) (*http.Request, error) {
file, err := os.Open(mypath)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, path.Base(mypath))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
contentType := writer.FormDataContentType()
err = writer.Close()
if err != nil {
return nil, err
}
//fmt.Printf("%s\n",body)
fmt.Printf("%s\n\n", contentType)
req, err := http.NewRequest("POST", uri, body)
req.Header.Add("Content-Type", contentType)
return req, err
}
func main() {
dummyfilePath := "./test.text"
authAPIURL := "https://www.google.com"
authResource := "/accounts/ClientLogin"
authData := url.Values{}
authData.Add("Email", "test@gmail.com")
authData.Add("Passwd", "password")
authData.Add("accountType", "GOOGLE")
authData.Add("service", "writely")
authData.Add("source", "cURL")
v, _, err := connectPOST(authAPIURL, authResource, authData, nil)
var authkey string
if err == nil {
re := regexp.MustCompile("Auth=(.*)")
authkey = re.FindStringSubmatch(v)[1]
} else {
return
}
authToken := fmt.Sprintf("GoogleLogin auth=%s", authkey)
mapLocation := map[string]string{
"Content-Length": "0",
"Authorization": authToken,
"GData-Version": "3.0",
"Slug": dummyfilePath,
}
uploadURL := "https://docs.google.com"
uploadResource := "/feeds/upload/create-session/default/private/full"
_, mapRes, err := connectPOST(uploadURL, uploadResource, authData, mapLocation)
if err == nil {
uploadLocation := mapRes.Get("Location")
fmt.Printf("%s\n", uploadLocation)
request, err := newfileUploadRequest(uploadLocation, "file", dummyfilePath)
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
request.Header.Add("Authorization", authToken)
request.Header.Add("GData-Version", "3.0")
request.Header.Add("Slug", dummyfilePath)
resp, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(body)
}
}
}