-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress.go
104 lines (94 loc) · 2.89 KB
/
wordpress.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type WordpressData struct {
Title string `json:"title"` //"My New Blog", // Blog Title
Content string `json:"content"` //"My first blog created with the Wordpress REST API", // Blog Description
Slug string `json:"slug"`
Author string `json:"author"`
PublicizeMessage string `json:"publicize_message"`
Status string `json:"status"`
Categories []string `json:"categories"`
Tags []string `json:"tags"`
}
func NewWordpressData(title string, content string) *WordpressData {
wD := WordpressData{
Title: title,
Content: content,
Author: "muellermh",
PublicizeMessage: "test",
Status: "draft",
Slug: "",
Categories: []string{"Allgemein"},
Tags: []string{},
}
return &wD
}
type Wordpress struct {
Endpoint string `json:"endpoint"`
Site string
Token string
}
func NewWordpress(config *ConfigBot) *Wordpress {
if !config.ActiveWordpress {
return nil
}
wp := Wordpress{Endpoint: "https://public-api.wordpress.com/rest/v1.1", Site: "digital-business.blog", Token: config.OauthWordpressToken.AccessToken}
return &wp
}
func (wp *Wordpress) CreateBlog(data *WordpressData) string {
oauth := AuthConfig{}
token, _ := oauth.LoadJSON("wordpress.oauth.json")
if token != nil && token.Expiry.Before(time.Now()) {
go Openbrowser("http://localhost:30000/wordpress")
}
client := &http.Client{}
jsonData, err := json.Marshal(data)
if err != nil {
return err.Error()
}
req, err := http.NewRequest("POST", wp.Endpoint+"/sites/"+wp.Site+"/posts/new/", bytes.NewBuffer(jsonData))
if err != nil {
return err.Error()
}
fmt.Println(data)
reqME, err := http.NewRequest("GET", wp.Endpoint+"/rest/v1/me/", bytes.NewBuffer(jsonData))
if err != nil {
return err.Error()
}
reqME.Header.Set("Content-Type", "application/json")
reqME.Header.Add("Authorization", "Bearer "+wp.Token)
respMe, err := client.Do(reqME)
if err != nil {
return err.Error()
}
bME, err := io.ReadAll(respMe.Body)
// b, err := ioutil.ReadAll(resp.Body) Go.1.15 and earlier
if err != nil {
LogError("konnte antwort nicht lesen", err)
return err.Error()
}
fmt.Printf("Status: %d, Body: %s", respMe.StatusCode, string(bME))
fmt.Println(wp.Token)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+wp.Token)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failed to create blog")
return err.Error()
}
fmt.Println("Successfully created blog")
b, err := io.ReadAll(resp.Body)
// b, err := ioutil.ReadAll(resp.Body) Go.1.15 and earlier
if err != nil {
LogError("konnte antwort nicht lesen", err)
return err.Error()
}
return fmt.Sprintf("Status: %d, Body: %s", resp.StatusCode, string(b))
}