Merhabalar;
Bir program yazmaya çalışıyorum. Belirleyeceğim bir klasörün içindeki resimleri, belirleyeceğim zaman aralıklarında, belirleyeceğim etiketlerle sosyal medya'da paylaşımı sağlayacak.
Öncelikle instagram ile başlamak istedim. Tabi bunun için öncelikle login olmak gerekiyor. Araştırmalarım sonucunda bir çok koda ve önerilere rastladım fakat hiç birinden sonuç alamadım.
Örneğin aşağıdaki kodlarda token alma, idhttp ile bağlanma, netclient ile bağlanma ve web browser ile bağlanmaya çalıştım.
Twebbrowser ile bağlanmada operation pointer hatası veriyor (Bu seçenekte giriş yapıyor fakat benim yazdığım kullacıya değil de açık olan kullanıcıya bağlanıyor)
idhttp ile bağlanmada bad request hatası veriyor.
netclient bağlanmada operation pointer hatası veriyor.
token al'da ise hiç hata vermiyor takılıp kalıyor.
Sadece login için yardımcı olur musunuz?
Bir program yazmaya çalışıyorum. Belirleyeceğim bir klasörün içindeki resimleri, belirleyeceğim zaman aralıklarında, belirleyeceğim etiketlerle sosyal medya'da paylaşımı sağlayacak.
Öncelikle instagram ile başlamak istedim. Tabi bunun için öncelikle login olmak gerekiyor. Araştırmalarım sonucunda bir çok koda ve önerilere rastladım fakat hiç birinden sonuç alamadım.
Örneğin aşağıdaki kodlarda token alma, idhttp ile bağlanma, netclient ile bağlanma ve web browser ile bağlanmaya çalıştım.
Twebbrowser ile bağlanmada operation pointer hatası veriyor (Bu seçenekte giriş yapıyor fakat benim yazdığım kullacıya değil de açık olan kullanıcıya bağlanıyor)
idhttp ile bağlanmada bad request hatası veriyor.
netclient bağlanmada operation pointer hatası veriyor.
token al'da ise hiç hata vermiyor takılıp kalıyor.
Sadece login için yardımcı olur musunuz?
unit fmfb;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls, OleCtrls, SHDocVw, Vcl.ExtCtrls, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent, IdCookie,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
IdServerIOHandler, IdSSL, IdSSLOpenSSL, IdIOHandlerSocket,
IdIOHandlerStack, IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, Vcl.Buttons,
IdURI, IdCookieManager;
type
TForm1 = class(TForm)
Panel2: TPanel;
Button1: TButton;
Web: TWebBrowser;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MySSL: TIdSSLIOHandlerSocketOpenSSL;
HTTP: TIdHTTP;
IdCookieManager1: TIdCookieManager;
implementation
{$R *.dfm}
function HTMLDecode(const AStr: String): String;
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <> #0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of
'a': if AnsiStrPos(Sp, 'amp;') = Sp then { do not localize }
begin
Inc(Sp, 3);
Rp^ := '&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp, 'gt;') = Sp) then { do not localize }
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <> ';') and (Sp^ <> #0) do
Inc(Sp);
if Cp^ = 'l' then
Rp^ := '<'
else
Rp^ := '>';
end;
'n': if AnsiStrPos(Sp, 'nbsp;') = Sp then { do not localize }
begin
Inc(Sp, 4);
Rp^ := ' ';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then { do not localize }
begin
Inc(Sp,4);
Rp^ := '"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <> ';') and (Sp^ <> #0) do
Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
Rp^ := Chr((I));
end;
else
Exit;
end;
end
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
end;
SetLength(Result, Rp - PChar(Result));
end;
function Pars(T_, ForS, _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 Auth(LoginString:string;OAuth_token:string):Boolean;
var s:TStringStream;
res,middlewaretoken, home, key:string;
han:Boolean;
Data:TStringList;
uri:TIdURI;
HTTP:TidHTTP;
begin
Result:=False;
s:=TStringStream.Create('');
Data:=TStringList.Create;
uri:=TIdURI.Create('https://instagram.com');
try
HTTP := TidHTTP.Create(nil);
MySSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
han := HTTP.HandleRedirects;
HTTP.HandleRedirects:=True;
HTTP.Request.Referer:='https://instagram.com/accounts/login/?next=/oauth/authorize/%3Fclient_id%3D'+OAuth_Token+'%26redirect_uri%3Dht tp%3A//localhost%26response_type%3Dtoken%26scope%3Drelations hips';
s.Size:=0;
try
HTTP.IOHandler:=MySSL;
HTTP.Get('https://api.instagram.com/oauth/authorize/?client_id='+OAuth_token+'&redirect_uri='+'http://localhost'+'&response_type=token&scope=relationships+likes+comments',s);
except
end;
res:=s.DataString;
middlewaretoken:=Pars('<input type="hidden" name="csrfmiddlewaretoken" value="',res,'"');
Data.Add('csrfmiddlewaretoken='+middlewaretoken);
Data.Add('username='+Trim(Copy(LoginString,1,Pos(' :',LoginString)-1)));
Data.Add('password='+Trim(Copy(LoginString,Pos(':' ,LoginString)+1,Length(LoginString))));
HTTP.Request.CustomHeaders.AddValue('Accept-Language', 'ru-RU');
HTTP.Request.CustomHeaders.AddValue('Referer', 'https://instagram.com/');
s.Size:=0;
HTTP.HandleRedirects:=False;
try
HTTP.Post('https://instagram.com'+Pars('<form method="POST" id="login-form" class="adjacent" action="',res,'"'),Data,s);
except
on E:EIdHTTPProtocolException do
begin
if (E.ErrorCode=302)and(Pos('access_token=',HTTP.Response.Location)>0) then
begin
key:=Pars('access_token=',HTTP.Response.Location, '');
Result:=True;
Exit;
end
else
if (e.ErrorCode=302) then
try
HTTP.HandleRedirects:=True;
try
HTTP.Get(HTTP.Response.Location,s);
except
end;
finally
HTTP.HandleRedirects:=False;
end;
end;
on E:Exception do;
end;
res:=s.DataString;
Data.Clear;
Data.Add('csrfmiddlewaretoken='+middlewaretoken);
Data.Add('allow=Authorize');
try
HTTP.Post('https://instagram.com/oauth/authorize/?client_id='+OAuth_Token+'&redirect_uri=http://localhost&response_type=token&scope=relationships+likes+comments',Data,s);
except
on E:EIdHTTPProtocolException do
begin
if (E.ErrorCode=302)and(Pos('access_token=',HTTP.Response.Location)>0) then
begin
showmessage('true');
key:=Pars('access_token=',HTTP.Response.Location, '');
Result:=True;
Exit;
end
else
if (e.ErrorCode=302) then
try
showmessage('false');
HTTP.Get(HTTP.Response.Location,s);
except
end;
end;
on E:Exception do;
end;
finally
HTTP.HandleRedirects:=han;
Data.Free;
uri.Free;
s.Free;
end;
end;
procedure WB_send_Click_by_Value(WB: TWebbrowser;form_nr:nativeint;tag,typ,val: string);
var ovElements: OleVariant;
i : integer;
Begin
ovElements := WB.OleObject.Document.forms.item(form_nr).elements;
for i := 0 to (ovElements.Length - 1) do
begin
if AnsiSameText(ovElements.item(i).tagName,tag) and
AnsiSameText(ovElements.item(i).type,typ) and
AnsiSameText(ovElements.item(i).value,val) then
ovElements.item(i).Click;
end;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
gelen,gelen2,gelen3:string;
label
bagli;
begin
web.Navigate('https://www.instagram.com/accounts/login/?force_classic_login');
while web.ReadyState <> READYSTATE_COMPLETE do
begin
sleep(1);
Application.ProcessMessages;
end;
Web.OleObject.Document.GetElementByid('email').value:=edit1.Text;
Web.OleObject.Document.GetElementByid('pass').value:=edit2.Text;
WB_send_Click_by_Value(Web,0,'input','submit','Giriş Yap');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
lHTTP: TNetHTTPClient;
httpreq: TNetHTTPRequest;
Params : TStrings;
Reply, Token: string;
s : TNetHeaders;
sReq : TStringStream;
// XSO : ISuperObject;
begin
Params := TStringList.Create;
try
Params.Add('username=' + Edit1.Text);
Params.Add('password=' + Edit2.Text);
lHTTP := TNetHTTPClient.Create(nil);
try
lHTTP.HandleRedirects := True;
lHTTP.Get('https://www.instagram.com', TStream(nil));
lHTTP.AllowCookies := True;
lHTTP.ContentType := 'application/x-www-form-urlencoded';
lHTTP.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
Token := lHTTP.CookieManager.Cookies[0].Value;
sReq := TStringStream.Create(Memo1.Text,TEncoding.UTF8);
sReq.Position := 0;
SetLength(s,5);
s[0].Name := 'X-CSRFToken';
s[0].Value := 'www.instagram.com' + Token;
s[1].Name := 'username=';
s[1].Value := Edit1.Text;
s[2].Name := 'password=';
s[2].Value := Edit2.Text;
s[3].Name := 'X-Instagram-AJAX';
s[3].Value := '1';
s[4].Name := 'X-Requested-With';
s[4].Value := 'XMLHttpRequest';
s[5].Name := 'Referer';
s[5].Value := 'https://www.instagram.com/';
Reply := lHTTP.Post('https://www.instagram.com/accounts/login/ajax/','',Nil,s).ContentAsString(TEncoding.UTF8);
Reply := '{"arr":'+Reply+'}';
// XSO := SO(Reply);
Memo1.Text := Reply;
finally
lHTTP.Free;
end;
finally
Params.Free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
lHTTP: TIdHTTP;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
Params : TStrings;
Reply, Token: string;
Cookie: TIdCookie;
begin
Params := TStringList.Create;
try
Params.Add('username=' + Edit1.Text);
Params.Add('password=' + Edit2.Text);
lHTTP := TIdHTTP.Create(nil);
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmClient;
lHTTP.IOHandler := IdSSL;
lHTTP.ReadTimeout := 30000;
lHTTP.HandleRedirects := True;
// capture cookies first...
// passing nil to ignore any response body data and not waste memory for it...
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.Get('https://www.instagram.com', TStream(nil));
Cookie := lHTTP.CookieManager.CookieCollection.Cookie['csrftoken', 'www.instagram.com'];
if Cookie <> nil then
Token := Cookie.Value;
// now submit the login webform...
lHTTP.Request.CustomHeaders.Values['X-CSRFToken'] := '8b474ece03msh51596fb843650d4p1d6533jsn0ad338e3d755';
lHTTP.Request.CustomHeaders.Values['X-Instagram-AJAX'] := '1';
lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
lHTTP.Request.Referer := 'https://www.instagram.com/';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
Reply := lHTTP.Post('https://instagram.com/accounts/login/', Params);
Memo1.Text := Reply;
finally
lHTTP.Free;
end;
finally
Params.Free;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Auth('login:xxxxxxxxx','OAuth_Token');
end;
end.


