forked from simonedegiacomi/gphotosuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.go
54 lines (44 loc) · 1.17 KB
/
simple.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
package main
import (
"fmt"
"os"
"github.com/GaPhi/gphotosuploader/api"
"github.com/GaPhi/gphotosuploader/auth"
)
// Simple example which consist in the upload of a single image
func main() {
// Load cookie for credentials from a json file
credentials, err := auth.NewCookieCredentialsFromFile("auth.json")
if err != nil {
panic(err)
}
// Get a new API token using the TokenScraper from the api package
token, err := api.NewAtTokenScraper(*credentials).ScrapeNewAtToken()
if err != nil {
panic(err)
}
// Add the token to the credentials
credentials.RuntimeParameters.AtToken = token
// Open the file to upload
file, err := os.Open("path/to/image.png")
if err != nil {
panic(err)
}
// Create an UploadOptions object that describes the upload.
options, err := api.NewUploadOptionsFromFile(file)
if err != nil {
panic(err)
}
// Create an upload using the NewUpload method from the api package
upload, err := api.NewUpload(options, *credentials)
if err != nil {
panic(err)
}
// Finally upload the image
uploadRes, err := upload.Upload()
if err != nil {
panic(err)
}
fmt.Println("success! image uploaded: ", uploadRes.URLString())
// Image uploaded!
}