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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, EncdDecd, IdHttp, StdCtrls, IdSSLOpenSSL;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button2Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
Const
ConsumerKey: String = 'd5TXrGHSb8PfG1iMKsnTVg'; {Valid Credentials for a temporary Twitter Test Application}
ConsumerSecret: String = 'UAtCg1c4nRGpDfUQfglqEHyRpVwwy8Mz2txZCq4O4s';
RessourcePost: String = 'https://api.twitter.com/oauth2/token?grant_type=client_credentials';
RessourceHost: String = 'api.twitter.com';
ContentType: String = 'application/x-www-form-urlencoded;charset=UTF-8';
implementation
{$R *.dfm}
Function EncodeBase64(Input: String): String;
Var
fstream, fStringStream: TStringStream;
Begin
fstream := TStringStream.Create(Input);
fStringStream := TStringStream.create('');
EncodeStream(fstream, fStringStream);
Result := fStringStream.DataString;
End;
procedure TForm1.Button2Click(Sender: TObject);
Var
MyHttp: TIdHttp;
EncodedCredit: String;
Result: TMemoryStream;
Userpath: String;
begin
{Step 1: Encode consumer key and secret
https://dev.twitter.com/docs/auth/application-only-auth}
EncodedCredit := EncodeBase64(ConsumerKey + ':' + ConsumerSecret);
{Step 2: Obtain a bearer token
https://dev.twitter.com/docs/auth/application-only-auth}
MyHttp := TIdHttp.Create(nil);
try
MyHttp.Request.ContentType := ContentType;
MyHttp.Request.Accept := 'application/json';
MyHttp.Request.Method := 'Post';
MyHttp.Request.BasicAuthentication := False;
MyHttp.Request.CustomHeaders.Add('Authorization: Basic '+EncodedCredit);
MyHttp.Request.Host := RessourceHost;
MyHttp.Request.URL := RessourcePost;
{Temporary load the Library for OpenSSL}
Userpath := 'J:\';
LoadLibrary(PWideChar(Userpath+'libeay32.dll'));
LoadLibrary(PWideChar(Userpath+'ssleay32.dll'));
MyHttp.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
Result := TMemoryStream.Create;
MyHttp.Post(RessourcePost, Result);
Memo1.Lines.LoadFromStream(Result);
finally
FreeAndNil(Result);
MyHttp.Free;
end;
end;
end. |
Partager