-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathVK.Classes.pas
More file actions
155 lines (140 loc) · 3.71 KB
/
VK.Classes.pas
File metadata and controls
155 lines (140 loc) · 3.71 KB
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
147
148
149
150
151
152
153
154
unit VK.Classes;
interface
uses
System.Classes, IdGlobal, IdHTTP, IdSSLOpenSSL;
type
TVKGroup = class
private
FHTTP: TIdHTTP;
FGroupID: string;
FLogin: string;
FPassword: string;
FToken: string;
FLastPostId: string;
FTokenAllow: Boolean;
function GetToken(var AToken: string): Boolean;
public
property TokenAllow: Boolean read FTokenAllow;
property LastPostId: string read FLastPostId;
function Post(Text: string; Attach: string = ''): Boolean;
function DeletePost(PostId: string): Boolean;
function GetLastPostId(var PostId: string): Boolean;
function Open(const Login, Password, GID: string): Boolean;
constructor Create;
end;
implementation
{ TVKGroup }
constructor TVKGroup.Create;
begin
FTokenAllow := False;
FLastPostId := '';
FHTTP := TIdHTTP.Create(nil);
FHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
FHTTP.IOHandler.MaxLineAction := maException;
FHTTP.IOHandler.Port := 0;
TIdSSLIOHandlerSocketOpenSSL(FHTTP.IOHandler).DefaultPort := 0;
FHTTP.AllowCookies := True;
FHTTP.HandleRedirects := True;
FHTTP.HTTPOptions := [hoForceEncodeParams];
end;
function Pars(st, st_begin, st_end: string): string;
begin
if Pos(st_begin, st) <> 0 then
Delete(st, 1, Pos(st_begin, st) + Length(st_begin) - 1)
else
Exit('');
if Pos(st_end, st) <> 0 then
Delete(st, Pos(st_end, st), Length(st))
else
Exit('');
Result := st;
end;
function TVKGroup.DeletePost(PostId: string): Boolean;
var
Response: string;
begin
Result := False;
try
Response := FHTTP.Get('https://api.vk.com/method/wall.delete?owner_id=-' + FGroupId + '&post_id=' + PostId + '&access_token=' + FToken);
except
Exit;
end;
Result := True;
end;
function TVKGroup.GetLastPostId(var PostId: string): Boolean;
var
Response: string;
begin
Result := False;
try
Response := FHTTP.Get('https://api.vk.com/method/wall.get?owner_id=-' + FGroupId + '&access_token=' + FToken + '&offset=1');
except
Exit;
end;
PostId := Pars(Response, '"id":', ',"');
Result := PostId <> '';
end;
function TVKGroup.GetToken(var AToken: string): Boolean;
var
Response, temp: string;
begin
Result := False;
try //2274003 - hHbZxrka2uZ6jB1inYsH
Response := FHTTP.Get('https://oauth.vk.com/token?grant_type=password&client_id=2274003&scope=wall&client_secret=hHbZxrka2uZ6jB1inYsH&username=' + FLogin + '&password=' + FPassword);
except
Exit;
end;
if Pos('token', Response) = 0 then
Exit;
temp := Copy(Response, Pos('access_token":"', Response), Pos('","expires_in', Response) - 3);
Delete(temp, 1, 15);
AToken := temp;
Result := True;
end;
function TVKGroup.Open(const Login, Password, GID: string): Boolean;
begin
Result := False;
FLogin := Login;
FPassword := Password;
FGroupID := GID;
try
FTokenAllow := GetToken(FToken);
except
Exit;
end;
Result := FTokenAllow;
end;
function TVKGroup.Post(Text, Attach: string): Boolean;
var
Response: string;
Info: TStringList;
begin
Result := False;
try
Info := TStringList.Create;
try
Info.Add('owner_id=-' + FGroupId);
Info.Add('from_group=1');
Info.Add('access_token=' + FToken);
Info.Add('message=' + Text);
Info.Add('attachments=' + Attach);
Response := FHTTP.Post('https://api.vk.com/method/wall.post', Info);
finally
Info.Free;
end;
except
Exit;
end;
if Pos('post_id', Response) > 0 then //Response '{"response":{"post_id":466}}'
begin
Delete(Response, Length(Response) - 1, 2);
FLastPostId := Copy(Response, Pos('post_id":', Response) + 9, 10);
Result := True;
end
else
begin
FLastPostId := '';
Result := False;
end;
end;
end.