D
D
drka2014-11-09 00:01:32
Delphi
drka, 2014-11-09 00:01:32

How to upload a VKApi photo correctly?

Guys such a problem. I upload photos to the VK group as it is said in the api documentation. Everything is going well, but after I get a link to upload a photo and make a request, the error "Security Breach2" crashes.

response:=send('GET', 'https://api.vk.com/method/photos.getUploadServer.xml?group_id='+group_id[ComboBox1.ItemIndex+1]+'&album_id='+albums_id[ComboBox2.ItemIndex+1]+'&v=5.26&access_token='+Unit4.token+''); // запрос на получение урла для загрузки фото
 
    temp_str:=response; 
    RegExpr.Expression:='<upload_url>(.*?)\.*?</upload_url>';
 
    if RegExpr.Exec(temp_str) then
      upload_url:=RegExpr.Match[1];  // выдираю из запроса урл
 
    dir:='6giMmEyPxNs.jpg'; // корневая папка программы с фото
    response:=send('POST',trim(upload_url),'','',False,'','','','','file1,'+dir+',image/jpeg','Mozilla/5.0 (Linux; U; Android 2.3.3; ko-kr; LG-LU3000 Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',''); //отсылаю фото
    ShowMessage(response); //Тут получаю вышеизложенную ошибку

I am using synapse+ lib. Also tried Indy same result

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Archakov Dennis, 2015-04-16
@archakov06

I'm too lazy to remake the code so that it fits your program. I think the meaning of the code will be clear if you yourself could write the code above.
PS: I wrote this code about two years ago and there is a lot of superfluous stuff, don't copy-paste it. Just understand the algorithm.

function Pars(ForS, T_, _T: string): string;
var
a, b: integer;
begin
Result := '';
if (T_ = '') or (ForS = '') or (_T = '') then
Exit;
a := Pos(T_, ForS);
if a = 0 then
Exit
else
a := a + Length(T_);
ForS := Copy(ForS, a, Length(ForS) - a + 1);
b := Pos(_T, ForS);
if b > 0 then
Result := Copy(ForS, 1, b - 1);
end;

function SendData(name,params: string):boolean;
var HTTP:THTTPSend;
begin
  HTTP:=THTTPSend.Create;
  HTTP.UserAgent:='Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1';
  HTTP.Protocol:='1.1';
  HTTP.HTTPMethod('GET', 'https://api.vk.com/method/'+name+'?'+params+'&access_token='+token);
  Response.LoadFromStream(HTTP.Document);
  Response.Text:=StringReplace(Response.Text,'\','',[rfreplaceall,rfignorecase]);
  HTTP.Free;
end;

var
FormData : TIdMultiPartFormDataStream;
d,g,Res,server,photos_list,hash:string;
vk:tvkclient;
HTTP:TIdHTTP;
begin
vk:=tvkclient.Create(nil);
vk.Login:='';
vk.Password:='';
vk.OnLogin;
token:=vk.VKToken;
cook:=vk.vk_cook;
SendData('photos.getUploadServer','album_id=122885845&group_id=46859739');

  if OpenDialog1.Execute then
  begin
  FormData := TIdMultiPartFormDataStream.Create;
  HTTP.Request.CustomHeaders.Add('Cookie: '+cook);
  HTTP.AllowCookies:=false;
  FormData.AddFile('file', OpenDialog1.FileName, 'multipart/form-data');
  g:=Response.Text;
  Res := HTTP.Post(Pars(g,'upload_url":"','"'), FormData);
  Res:=StringReplace(Res,'\','',[rfreplaceall,rfignorecase]);
  server:=Pars(Res,'server":',',');
  photos_list:=Pars(Res,'photos_list":"','","aid');
  hash:=Pars(Res,'hash":"','"');
  SendData('photos.save','album_id=166911641&group_id=46859739&server='+server+'&photos_list='+photos_list+'&hash='+hash);
  Mmo1.Text:=response.Text;
  FormData.Free;
  end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question