Advertisement
Guest User

Untitled

a guest
Dec 4th, 2013
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.60 KB | None | 0 0
  1. function PobierzTokenTwitch(login, haslo: string): string;
  2. var
  3.   IdHTTP: TIdHTTP;
  4.   IdSSL: TIdSSLIOHandlerSocketOpenSSL;
  5.   HTML, authToken: string;
  6.   parametry: TStringList;
  7.   poczatek, koniec: integer;
  8.   strumien: TMemoryStream;
  9.   fLogin, fHaslo: string;
  10. begin
  11.   fLogin := login;
  12.   fHaslo := haslo;
  13.   try
  14.     IdHTTP := TIdHTTP.Create;
  15.     try
  16.       IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
  17.       IdHTTP.IOHandler := IdSSL;
  18.       IdHTTP.AllowCookies := True;
  19.       IdHTTP.CookieManager := Form1.IdCookieManager1;
  20.       IdHTTP.HandleRedirects := True;
  21.       // Logowanie do Twitcha
  22.       HTML := IdHTTP.Get('http://www.twitch.tv/login');
  23.       poczatek := Pos('"authenticity_token" type="hidden" value="', HTML) +
  24.         Length('"authenticity_token" type="hidden" value="');
  25.       koniec := PosEx('"', HTML, poczatek);
  26.       authToken := copy(HTML, poczatek, koniec - poczatek);
  27.       // ShowMessage(authToken);
  28.       parametry := TStringList.Create;
  29.       try
  30.         parametry.Add('utf8=✓');
  31.         parametry.Add('authenticity_token=' + authToken);
  32.         parametry.Add('redirect_on_login=');
  33.         parametry.Add('embed_form=false');
  34.         parametry.Add('user[login]=' + login);
  35.         parametry.Add('user[password]=' + haslo);
  36.         try
  37.           strumien := TMemoryStream.Create;
  38.           IdHTTP.Post('https://secure.twitch.tv/user/login', parametry,
  39.             strumien, IndyUTF8Encoding);
  40.         except
  41.         end;
  42.         // Autoryzacja dla launchera
  43.         parametry.Clear;
  44.         parametry.Add('utf8=✓');
  45.         parametry.Add('authenticity_token=' + authToken);
  46.         parametry.Add('login_type=login');
  47.         parametry.Add('response_type=token');
  48.         parametry.Add('client_id=xxx');
  49.         parametry.Add('redirect_uri=http://localhost');
  50.         parametry.Add('scope=chat_login');
  51.         try
  52.           IdHTTP.Post('https://api.twitch.tv/kraken/oauth2/allow', parametry,
  53.             strumien, IndyUTF8Encoding);
  54.           IdHTTP.Get
  55.             ('https://api.twitch.tv/kraken/oauth2/authorize?response_type=token&client_id=xxx'
  56.             + '&redirect_uri=http://localhost&scope=chat_login');
  57.         except
  58.         end;
  59.       finally
  60.       // Wyodrębnienie tokena
  61.         poczatek := Pos('#access_token', IdHTTP.Response.Location);
  62.         if poczatek > 0 then
  63.           Result := 'oauth:' + copy(IdHTTP.Response.Location,
  64.             poczatek + 14, 31);
  65.         parametry.Free;
  66.         IdSSL.Free;
  67.         strumien.Free;
  68.       end;
  69.     finally
  70.       idHTTP.Free;
  71.     end;
  72.   except
  73.     on E: Exception do
  74.       ShowMessage(E.Message);
  75.   end;
  76. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement