Delphi Авторизация API Instagram

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by toxam9, 15 Jun 2014.

  1. toxam9

    toxam9 New Member

    Joined:
    22 May 2009
    Messages:
    19
    Likes Received:
    0
    Reputations:
    0
    Господа, подскажите пожалуйста, почему не могу получить access_token, вроде все правильно делаю. 2рые сутки не могу победить.

    Аккаунт валидный. На php все работает, но надо реализовать на Delphi.

    Code:
    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdIOHandler, IdIOHandlerSocket,
      IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
      IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, Vcl.StdCtrls, Vcl.Buttons,
      IdTCPConnection, IdTCPClient, IdHTTP, IdURI, IdCookieManager;
    
    type
      TForm1 = class(TForm)
        MySSL: TIdSSLIOHandlerSocketOpenSSL;
        HTTP: TIdHTTP;
        BitBtn1: TBitBtn;
        Memo1: TMemo;
        IdCookieManager1: TIdCookieManager;
    procedure BitBtn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    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);
        Form1.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:=Form1.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 TForm1.BitBtn1Click(Sender: TObject);
    begin
          Auth('login:password','OAuth_Token');
    end;
    
    
    
    end.
    
     
  2. toxam9

    toxam9 New Member

    Joined:
    22 May 2009
    Messages:
    19
    Likes Received:
    0
    Reputations:
    0
    Ребята, подскажите пожалуйста что делаю ни так?